Thread: help me!!!!

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    help me!!!!

    Ok, I have recently started on c++ and I wanted to make something like a bank. On my program I would be able to input a username, and a password and then I would get to input them again to login... It doesn't work. When I run the code I am about to show, the "user" (my username variable) and my "pass" (password variable) never seem to match the variables I put later when trying to login. I tried to leave messages to you so you could understand what I am saying here.
    Here it is:
    Code:
    #include<iostream> //do I need to add something here?
    using namespace std;
    
    //functions->
    void signup();
    char user[26];
    char pass[16];
    void login();
    void line();
    void transaction();
    // <-functions (and global variables)
    
    int main()
    {
    	signup();
    	login();
    }
    void signup() //seems to run fine... but I am new
    {	
    	cout<<"Enter your user: ";
    	cin.getline(user, 26);
    	line();
    	cout<<"Now your password: ";
    	cin.getline(pass, 16);
    	line();
    	cout<<"You entered "<<user<< " " <<pass<<"\n";
    }
    
    void line()
    {
    	cout<<"\n"; //I cheated at inserting lines everytime lol
    }
    void login()
    {
    	char x[26]; //to check the username and password 
    	char y[16]; //for login purposes... simple but problematic.
    	cout<<"Enter your user: ";
    	cin.getline(x, 26);
    	line();
    	cout<<"Enter your password: ";
    	cin.getline(y, 16);
    	line();
    	cout<<"You entered "<<user<< " " <<pass<<"\n";
    	if(x == (user)) //these seem to be my main issue
    	{
    		if(y == (pass)) //here too
    		{
    		line();
    		transaction();
    		}
    	}
    	else
    	{
    		cout<<"Wrong user or password\n"; //this runs every time even when i have typed the
    		login();                         //perfect matches for logins
    	}
    }
    void transaction()
    {
    	system("PAUSE");
    }
    Please help as soon as possible!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    OK, simple misconception. user, pass, x, and y are all arrays, char arrays to be more precise. When you use a char array's name by itself, what happens in C/C++ is that the array is treated as a pointer to the first element of the array.

    This means that when you compare x to user, you're comparing the address of the first element of array x to the address of the first element of array user. In this program, that will be false each and every time since user and x are located at different memory addresses.

    What you should do is use something like strcmp() to compare two C-style strings. While we're on ths subject of strings, you should look into the C++ std::string class. They are safer and easier to use than char arrays.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    You lost me in your last paragraph. I haven't made it to string compare yet. (I learn slowly) Could you possibly give me an example on what I need to replace and what to replace it with? Or, if you don't mind, could you copy and paste my code and edit it? No matter what you do next, thank you very much for your help. I will look up strcmpr now.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    A traditional C-style approach:

    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
    	char one[] = "bleh";
    	char two[] = "blah";
    	
    	if(strcmp(one,two) == 0)
    	{
    		std::cout << "Equal" << std::endl;
    	}
    	else std::cout << "Not not not equal!" << std::endl;
    	
    	return 0;
    }
    The C++ way to do it with the string class:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::string one("bleh");
    	std::string two("blah");
    	
    	if(one.compare(two) == 0)
    	{
    		std::cout << "Equal" << std::endl;
    	}
    	else std::cout << "Not not not equal!" << std::endl;
    	
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    Thank you sooooooooo much. I think I get it.
    Here is my new version. Messy, yes, but it also works!
    Code:
    #include<iostream> 
    #include<cstring>
    using namespace std;
    
    
    void signup();
    char user[26];
    char pass[16];
    void login();
    void line();
    void transaction();
    
    
    int main()
    {
    	signup();
    	login();
    }
    void signup() 
    {	
    	cout<<"Enter your user: ";
    	cin.getline(user, 26);
    	line();
    	cout<<"Now your password: ";
    	cin.getline(pass, 16);
    	line();
    	cout<<"You entered "<<user<< " " <<pass<<"\n";
    }
    
    void line()
    {
    	cout<<"\n"; 
    }
    void login()
    {
    	char x[26];  
    	char y[16]; 
    	cout<<"Enter your user: ";
    	cin.getline(x, 26);
    	line();
    	cout<<"Enter your password: ";
    	cin.getline(y, 16);
    	line();
    	cout<<"You entered "<<user<< " " <<pass<<"\n";
    	if( strcmp (user, x) == 0)
    	{
    		if( strcmp (pass, y) == 0)
    		{
    			transaction();
    		}
    	}
    	else
    	{
    		cout<<"Wrong\n";
    	login();
    	}
    }
    void transaction()
    {
    	system("PAUSE");
    }

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Nice name shadowsora15. Reminds my of Anti-Form in Kingdom Hearts 2. Anyhow I would recommend you switch from arrays to std::string's. strings are more stable, and alot easier to work with.
    which looks easier?
    Code:
    char x[26];  
    	char y[16]; 
    	cout<<"Enter your user: ";
    	cin.getline(x, 26);
    	line();
    	cout<<"Enter your password: ";
    	cin.getline(y, 16);
    	line();
    	cout<<"You entered "<<user<< " " <<pass<<"\n";
    	if( strcmp (user, x) == 0)
    	{
    		if( strcmp (pass, y) == 0)
    		{
    			transaction();
    		}
    	}
    	else
    	{
    		cout<<"Wrong\n";
    	login();
    	}
    or
    Code:
    std::string x;
        std::string y;
    	cout<<"Enter your user: ";
    	getline(cin, x, '\n');
    	cout << endl;
    	cout<<"Enter your password: ";
    	getline(cin, y, '\n');
    	cout << endl;
    	cout<<"You entered "<<user<< " " <<pass<<"\n";
    	if(user.compare(x) == 0)
    	{
    		if(pass.compare(y) == 0)
    		{
    			transaction();
    		}
    	}
    	else
    	{
    		cout<<"Wrong\n";
            login();
    	}
    Really I guess it is it up to preference in the end. I hate giving biased opinions. I just think that strings are easier than char arrays.
    also if you are going to use the line() function, you may want to make it inline so that it is a little quicker if you use it hundreds of times.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You are also missing return 0 from your int main. Also, you have declared thec char[] arrays global. This is fine as your are learning, but try not to get into the habbit of using global variables. Pass the arrays to whichever function(s) need them and make them local in the future.
    Double Helix STL

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    21
    Thanks for the adviece. I will start all of that on my next project, which I will start sometime today. At the moment I am having another problem. I keep getting an error called c2059 syntax "}". I checked it over and over again and nothing seems amiss.
    Here is the small section that is messed up:
    Code:
    void transaction()
    {
    	int depositorwithdraw;
    	float bankedcash;
    	float handcash;
    	float withdraw;
    	float deposit;
    	const int LOW = 100;
    	const int HIGH = 1000;
    	time_t seconds;
    	time(&seconds);
    	srand((unsigned int) seconds);
    	int x;
    	
    	cout<<"Welcome " << user << ".\nNice to see you.\n";
    	cout<<"Are you here to withdraw or deposit?\n";
    	cout<<"(Enter a \"1\" to deposit cash, or a \"2\" to withdraw. ";
    	cin>>depositorwithdraw;
    	line();
    		bankedcash = rand() % (HIGH - LOW + 1) + LOW;
    		handcash = rand() % (HIGH - LOW + 1) + LOW;
    	if(depositorwithdraw == 1)
    	{
    		do
    		{
    		do
    		{
    		cout<<"How much cash would you like to deposit?\n";
    		cin>>deposit;
    		if(deposit > handcash)
    		{
    			cout<<"Too much\n";
    		}
    		}
    		while(deposit > handcash);
    		bankedcash = bankedcash + deposit;
    		handcash = handcash - deposit;
    		cout<<"Your balance is now $" << bankedcash << ".";
    		line();
    		cout<<"You only have $" <<handcash<< " on you.";
    		cout<<"Insert a 1 to exit, or 0 to do again.\n";
    		cin>>x;
    		while(x != 1); //incase they want to redo it
    		}                   //problem started after I added it
    	} //Here is the problem.
    		if(depositorwithdraw == 2)
    	{
    		//withdraw
    	}
    	if(depositorwithdraw > 2 || depositorwithdraw < 1)
    	{
    		cout<<"Incorrect command";
    		line();
    	}
    	line();
    	system("PAUSE");
    }
    Again I know it isn't clean or anything but it is so problematic.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    the while bit on the do... while loop. It goes outside the do loop, ie:

    Code:
    do{
         ....
    }while(whatever);
    Your second do...while() loop has the while inside the loop

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are also missing return 0 from your int main.
    That is optional.

    Here is the small section that is messed up:
    You need to indent your code properly. Here is an example:
    Code:
    void transaction()
    {
        int depositorwithdraw;
        float bankedcash;
        float handcash;
        float withdraw;
        float deposit;
        const int LOW = 100;
        const int HIGH = 1000;
        time_t seconds;
        time(&seconds);
        srand((unsigned int) seconds);
        int x;
    
        cout<<"Welcome " << user << ".\nNice to see you.\n";
        cout<<"Are you here to withdraw or deposit?\n";
        cout<<"(Enter a \"1\" to deposit cash, or a \"2\" to withdraw. ";
        cin>>depositorwithdraw;
        line();
        bankedcash = rand() &#37; (HIGH - LOW + 1) + LOW;
        handcash = rand() % (HIGH - LOW + 1) + LOW;
        if(depositorwithdraw == 1)
        {
            do
            {
                do
                {
                    cout<<"How much cash would you like to deposit?\n";
                    cin>>deposit;
                    if(deposit > handcash)
                    {
                        cout<<"Too much\n";
                    }
                }
                while(deposit > handcash);
                bankedcash = bankedcash + deposit;
                handcash = handcash - deposit;
                cout<<"Your balance is now $" << bankedcash << ".";
                line();
                cout<<"You only have $" <<handcash<< " on you.";
                cout<<"Insert a 1 to exit, or 0 to do again.\n";
                cin>>x;
                while(x != 1); //incase they want to redo it
            }                   //problem started after I added it
        } //Here is the problem.
        if(depositorwithdraw == 2)
        {
            //withdraw
        }
        if(depositorwithdraw > 2 || depositorwithdraw < 1)
        {
            cout<<"Incorrect command";
            line();
        }
        line();
        system("PAUSE");
    }
    With the code properly indented, it is easy to see that you have misplaced your while keyword such that your outer do while loop now has a do with no while. You need to shift the while after the closing brace of the do.

    Incidentally, you might want to seed your pseudorandom number generator once, rather than have it seeded every time the function is run.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed