简体   繁体   中英

Global variable with char

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.

In your header file, you need to declare the variable "extern"; ie, extern char username[50];

That means "Here's the type and the name of the variable, but it's defined somewhere else".

If you declare it without the "extern", then each file that includes your header file will end up with a global variable named "username", and if you're lucky, then your linker will complain about duplicate variables.

Along with declaring it "extern" in your header file, you need to declare it (not extern) in one source file.

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;

The main function is where your program begins. The first thing you do when you program starts is:

cout << username;

This will immediately output the contents of the username , which at the moment you have not filled with a username. Make sure you do the user input before you start outputting the username. This (if my psychic abilities are correct) will require calling the function you're defining in login.cpp. So whatever that function is called, call it: for example:

int main()
{
  login();
  cout << username << endl;
}

In your login.cpp file, you have this if-else statement without any enclosing function. I'm not sure if this just because you copied only part of the file, but those statements need to be within a function. You will probably need to declare that function in a header file if you want to use it in other files.

To share the username global variable between different translation units you will need to declare it in a header file, as extern char username[50] and then define it in only one of your implementation files as char username[50] .

I, however, do not recommend using a global variable to this and suggest that you use std::string instead of char arrays. You can simply return the username out of the login function and do something like this:

int main()
{
  std::string username = login();
  std::cout << username << std::endl;
}

In reference to the comments on this post:

std::string login()
{
  // Here we get the user's username and return it back to main
  std::string username;
  std::cin >> username;
  return username;
}

void menu(std::string username)
{
  std::cout << "Welcome " << username << std::endl;
}

int main()
{
  std::string username = login(); // We receive the username from login()
  menu(username); // We pass the username we received to menu()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM