CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2013
    Posts
    7

    help with c++ code if else statement

    I made a code to put in the user name id and password then create a new password; if the user name&password are wrong it should come up with a loop saying "invalid user id you can try ___ number of times. Please enter user id: " Then it starts over again so that part works in my code. But when making a new password it should not loop; it should only be 4 digits and can not be the same password of 1234; and this is where i am having a problem the 1234 works and says "Your password could not be changed." but when i put anything over or under 4 digits it doesnt do anything. it should say "Your password could not be changed." and exit but i'm not sure what i'm doing wrong can somebody please help me with this code and tell me how to fix it please!??

    #include <iostream>
    using namespace std;
    int main()
    { //declaration
    int pw;
    int id;
    int i=3;


    do{

    cout<<"enter six digit id: ";
    cin>>id;
    cout<<"enter four digit password: ";
    cin>>pw;


    if(123456==id && 1234==pw)
    {
    cout<<"Succesful login, please change password.”;
    cout<<"Enter new 4-digit password: ";
    cin>>pw;


    if ((pw >= 1000 && pw <= 9999 && pw != 1234))
    {
    cout<<"password has been changed."<< endl;
    break;

    }
    else if (pw == 1234 && pw > 1000 && pw < 10000)
    cout<<"password could not be changed."<<endl;
    break;

    }


    else
    {
    cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
    if (0==i)
    cout<<"Login failed\n";


    i--;
    }
    }while(i>=0);



    system("pause");
    }

  2. #2
    Join Date
    Nov 2011
    Posts
    7

    Re: help with c++ code if else statement

    Hi wazup6678,

    I'm not a professional programmer, but while I was reading through your code, everything looks okay. The only thing I noticed is that your main method is not returning anything. I'm not sure if that will fix it, but I was taught that main always needs a return type. At the bottom of your system("PAUSE"); try adding return 0;

    Also, for this section. You are not subtracting i.

    Code:
    else
    {
    cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
    if (0==i)
    cout<<"Login failed\n";
    Should be this:

    Code:
    else
    {
    cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
    i--;
    if (i == 0)
    cout<<"Login failed\n";

    I hope this somewhat helped you. If not, I apologize. I'm still learning myself.

    Best of luck,

    SubZeroGaming

  3. #3
    Join Date
    Nov 2011
    Posts
    7

    Re: help with c++ code if else statement

    Here you go. I tested it in Visual Studios. Fixed a few errors. Also, for future reference, please, please use conventions.

    Works

    NOTE:

    This line here is not 100% how it should work, but the program does its job. This will give you something to fiddle with.

    Code:
    else if (pw == 1234 && pw > 1000 && pw < 10000) 
    		{
    			cout<<"password could not be changed."<<endl;
    			break;
    		}
    Enjoy.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    { //declaration
    
    	int pw;
    	int id;
    	int i=3;
    
    
    	do{
    
    	cout<<"enter six digit id: ";
    	cin>>id;
    	cout<<"enter four digit password: ";
    	cin>>pw;
    
    
    		if(123456==id && 1234==pw)
    		{
    			cout << "Successful login, please change password.";
    			cout << "Enter new 4-digit password: ";
    			cin>>pw;
    		}
    
    		if ((pw >= 1000 && pw <= 9999 && pw != 1234))
    		{
    			cout<<"password has been changed."<< endl;
    			break;
    		}
    		else if (pw == 1234 && pw > 1000 && pw < 10000) 
    		{
    			cout<<"password could not be changed."<<endl;
    			break;
    		}	
    		else
    		{
    			cout<<"Invalid user id/password you can try: "<<i<<" more times."<<endl;
    			i--;
    			if (i == 0)
    				cout<<"Login failed\n";
    			}
    		}
    	while(i>=0);
    
    
    	system("pause");
    	return 0;
    }
    Best of luck,

    SubZeroGaming
    Last edited by SubZeroGaming; June 17th, 2013 at 02:29 AM.

  4. #4
    Join Date
    Jun 2013
    Posts
    7

    Re: help with c++ code if else statement

    Thanks so much for your help ur awesome!! it worked great

  5. #5
    Join Date
    Nov 2011
    Posts
    7

    Re: help with c++ code if else statement

    Glad I could help.

    Best of luck,

    SubZeroGaming

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: help with c++ code if else statement

    Hmmm. As there is only one chance to change the password after a successful login, why put this code in the main loop? Also there is no checking for incorrect type of input. What if someone enters a Q for the id?

    Try this

    Code:
    #include <iostream>
    using namespace std;
    
    const int CORR_ID = 123456;
    const int CORR_PASS = 1234;
    const int MAX_LOGIN = 3;
    
    int main()
    {
    int	pw,
    	id,
    	i = MAX_LOGIN;
    
    bool	correct = false,
    	bad = true;
    
    	do {
    		do {
    			bad = false;
    			cout << "Enter six digit id: ";
    			if (!(cin >> id)) {
    				cout << "Invalid id entered" << endl;
    				cin.clear();
    				cin.ignore(100, '\n');
    				bad = true;
    			}
    		} while (bad == true);
    
    		do {
    			bad = false;
    			cout << "Enter four digit password: ";
    			if (!(cin >> pw)) {
    				cout << "Invalid password entered" << endl;
    				cin.clear();
    				cin.ignore(100, '\n');
    				bad = true;
    			}
    		} while (bad == true);
    
    		if (CORR_ID != id || CORR_PASS != pw) {
    			cout << "Incorrect user id/password.";
    			if (--i > 0) {
    				cout << " You can try: " << i << " more time(s).";
    			}
    			cout << endl;
    		} else {
    			correct = true;
    		}
    	} while (i > 0 && correct == false);
    
    	if (i == 0) {
    		cout << "Login failed\n";
    		return 1;
    	}
    
    	cout << "Successful login, please change password." << endl;
    	do {
    		bad = false;
    		cout << "Enter new 4-digit password: ";
    		if (!(cin >> pw)) {
    			cout << "Invalid password entered" << endl;
    			cin.clear();
    			cin.ignore(100, '\n');
    			bad = true;
    		}
    	} while (bad == true);
    
    	if (pw >= 1000 && pw <= 9999 && pw != 1234) {
    		cout << "Password has been changed."<< endl;
    	} else {
    		cout << "Password could not be changed." << endl;
    	}
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured