Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:36:42+00:00 2026-06-15T17:36:42+00:00

I have global variable. I want to use char data type so I can

  • 0

I have global variable. I want to use char data type so I can insert username in it. So far it’s not working.

In main.cpp

#include "Functions.h"

using namespace std;

    char username[50];

int main()
{   
    cout << username;
}

In Functions.h

char username[50];

In login.cpp

#include "Functions.h"

                if(std::strcmp(emp_username, "admin") == 1) {
                    username = "admin";

                }

                else
                {
                    username = emp_username;

                }

What I want to do is to get the employee username and display it in every function. It works with int data type. Only I don’t know how to use it with char. Please help me with this. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:36:43+00:00Added an answer on June 15, 2026 at 5:36 pm

    Don’t use plain character arrays to store strings, use the std::string class:

    std::string username;
    std::string emp_username;
    
    // Note that this is rather pointless, since it's equivalent to 
    //    username = emp_username;
    // but I'll leave it as it is to demonstrate how "string" works.
    if (emp_username == "admin") {
        username = "admin";
    } else {
        username = emp_username;
    }
    

    If you have some weird requirement that forces you to use plain arrays, then compare using:

    if (std::strcmp(emp_username, "admin") == 0)  // NOT 1
    

    and the contents can’t be assigned using =; you need library functions:

    username[sizeof username - 1] = 0;
    std::strncpy(username, emp_username, sizeof username);
    if (username[sizeof username - 1] != 0) {
        // Whoops! The buffer was too small. Handle the error somehow.
    }
    

    Finally, if you must have a global variable (which is nearly always a very bad idea), then you need to declare it extern in the header:

    extern std::string username;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a global pointer variable char* pointer = new char[500]; /* some operations...
I have the following global in variable in file_1.pl, in package main: package main;
I have a global variable in my javascript, whose value I want to set
If i have global variable in A.dll, that depends on global variable in B.dll
I have a global variable averagel that I am trying to manipulate in a
I have a global variable contacts, var contacts = ContactsApp.getContacts(); I have an KeyUphandler
I have a global variable that is an instance of my custom class. How
If I have a global static variable x like in this code #include <stdio.h>
In following program . I have one doubt. I have declared one global variable
I have a header file variable.h where i declare all my global variable.Then i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.