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.

3

3 Answers 3

2

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
2

In your header file, you need to declare the variable "extern"; i.e, 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.

1

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()
}
5
  • Actually that username is being assigned to emp_username at login.cpp` I can't believe you actually help me there with calling function. You are a good helper. This place is full with nice people :)
    – sg552
    Commented Dec 6, 2012 at 19:25
  • I actually want to do this. User login at login.cpp with username and password and redirect to main_menu.cpp. At mainmenu.cpp I will cout << "Welcome" << username. The only way I know to do that is with global variable and because global variable is only overwrite one time during login and the rest is getting the global variable to display "Welcome username" I think it is safe for me to use global variable. Thanks again.
    – sg552
    Commented Dec 6, 2012 at 19:49
  • Global variables are never necessary (and often considered bad design!). It sounds like what you really want is int main() { std::string username = login(); menu(username); } Commented Dec 6, 2012 at 19:55
  • I don't get it. At login.cpp I need to assign my employee username to username as usual and when I redirect to menu.cpp, menu(username) function will fetch the variable name?
    – sg552
    Commented Dec 6, 2012 at 20:18
  • @sg552 Check the code that I've added to my answer. You seem to not understand the idea of passing arguments to functions and returning objects from functions. Don't think about it as "redirecting to a file". Files have no purpose other than splitting up your code for organisation. You simply call functions, passing arguments to them and then return objects back. Commented Dec 6, 2012 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.