Thread: How to go back to previous line, to retry validating username and password?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    How to go back to previous line, to retry validating username and password?

    Expand the password checking program from earlier in this chapter and make it take multiple
    usernames, each with their own password, and ensure that the right username is used for the
    right password. Provide the ability to prompt user's again if the first login attempt failed. Think
    about how easy (or hard) it is to do this for a lot of usernames and passwords.

    From the book Jumping into C++ page 71 Q 4, chapter 4: If Statements.

    Here's what I've done so far:

    Code:
     //Answer to page 71 Q 4
    
    
    
    #include <iostream>;
    #include <string>;
    using namespace std;
    
    
    int main()
    
    
    {   string username;
        string password;
        string choice;
    
    
         
         top:
        
         cout << "Username: ";
         getline(cin, username, '\n');
        
        
        cout << "Password: ";
        getline( cin, password, '\n');
    
    
        if ( username == "Johnny" && password == "x" || username == "Debb" && password == "y" || username == "Oh hai" && password == "z")
        { cout << "Valid password. You may now enter paradise.\n"; }
    
    
        else 
        { cout << "Invalid username or password. Please try again.\n If you forgot your password, ask for help at nearby abyss's sinner support.\n\n";
          
          top2:
    
    
          cout << "Do you want to try filling in the username and password again? y/n \n\n";
          cin >> choice;
          
          if ( choice == "y")
          { goto top; }
    
    
          else if (choice == "n")
          { return 0; }
    
    
          else 
          { cout << "Invalid choice. Choices: y/n. \n\n";
            
            goto top2;
          }
        
        }
    
    
        system("pause");
        return 0;
    
    
    }
    My question now is, how do I prompt the user to retry validating their username and password if their previous attempt had failed?

    I tried using GOTO, but when it prompt the user the the previous line, the user cannot input their username.

    There must be many alternatives to do this.
    Please tell me what you know, thanks.

    The paradise and abyss part is just a joke. I don't mean to offend anybody. I've never met people go in or out of it though. So I think nobody will be offended. But just in case.

    Please do also tell me if there's any flaw in the structure of my code, and which part can be improved. Thank you.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First off, don't use goto. It causes spagetti code, as you can see, very hard to read. You should learn how to use loops.
    Now, as for the problem, it's basically a symptom of using std::getline after std::cin >>. Use std::cin.ignore after std::cin >> to get rid of data left in the input buffer that is consumed by the next std::getline.
    Oh, and try to use the "if fail, retry" approach instead of "if succeeded, then if succeeded, then ... ".
    Example (don't do this):

    Input password 1:
    If succeeded then
    - Input password 2:
    - If succeeded then
    - Print hello world message
    - End if
    End if

    Example (do this):
    Input password 1:
    If fail, restart
    Input password 2:
    If fail, restart
    Print hello world message

    Not only do you get rid of tons of nested ifs, it looks better and involves less complexity, too.
    Last edited by Elysia; 12-12-2012 at 07:02 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    I added cin.ignore(); after cin.getline. I now can input username and password again after failed the first attempt to validate username and password. However, even if I put the valid password on the second attempt, the username and password is invalid. Why?

    And sorry, I don't really understand what you meant by "if fail, retry" approach and "if succeeded, then if succeeded, then... " approach. Could you explain it in more detail, add example code perhaps?

    Here's the change I've made:

    Code:
     //Answer to page 71 Q 4, chapter 4: If Statements
    
    
    
    #include <iostream>;
    #include <string>;
    using namespace std;
    
    
    int main()
    
    
    {   string username;
        string password;
    	string choice;
    
    
    	 username:
         
    	
    	 cout << "Username: ";
         getline(cin, username, '\n');
    	 cin.ignore();
        
    
    
    	cout << "Password: ";
        getline( cin, password, '\n');
    	cin.ignore();
    
    
    	if ( username == "Johnny" && password == "x" || username == "Debb" && password == "y" || username == "Oh hai" && password == "z")
    	{ cout << "Valid password. You may now enter paradise.\n"; }
    
    
    	else 
    	{ cout << "Invalid username or password. Please try again.\n If you forgot your password, ask for help at nearby abyss's sinner support.\n\n";
          
          top2:
    
    
    	  cout << "Do you want to try filling in the username and password again? y/n \n\n";
    	  cin >> choice;
    	  
    	  if ( choice == "y")
    	  { goto username; }
    
    
    	  else if (choice == "n")
    	  { return 0; }
    
    
    	  else 
    	  { cout << "Invalid choice. Choices: y/n. \n\n";
    	    
    	    goto top2;
    	  }
    	
    	}
    
    
        system("pause");
    	return 0;
    
    
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    seriously. stop using goto! use a loop. do...while seems appropriate here. also, there is usually no need to use cin.ignore() after a call to std::getline().

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Sorry...Made a change after that. Just learned do while loops. Is this correct? Though I did use at least 1 goto because I dunno how to do 'do while' loop twice. Like this?

    I removed the cin.ignore() after getline(), but still I can't input the username, only can input password.

    Code:
    //Answer to page 71 Q 4, chapter 4: If Statements
    
    
    
    
    #include <iostream>;
    #include <string>;
    
    
    using namespace std;
    
    
    string username;
        string password;
    	string choice;
    
    
    
    
    
    
    
    
    
    
    int main()
    
    
    {   
    	
    	
    
    
    
    
    	
         do {
    	
    	 cout << "Username: ";
         getline(cin, username);
    	 
    	 
    	 cout << "Password: ";
         getline( cin, password);
    	
    
    
    	if ( username == "Johnny" && password == "x" || username == "Debb" && password == "y" || username == "Oh hai" && password == "z")
    	{ cout << "Valid password. You may now enter paradise.\n"; }
    
    
    	else 
    	{ cout << "Invalid username or password. Please try again.\n If you forgot your password, ask for help at nearby abyss's sinner support.\n\n";
          
          choice:
    
    
    	  cout << "Do you want to try filling in the username and password again? y/n \n\n";
    	  cin >> choice;
    	  
    	  
    	    
    	  }
    	}
    	   while ( choice == "y" && choice != "n");
    
    
    	   if ( choice != "y" && choice != "n" )
    	   { cout << "Invalid choice.\n";   
    	     goto choice;                } 
    	
    	
    
    
        system("pause");
    	return 0;
    
    
    }
    Thank you for replying.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    And I just found out that the while loop is on chapter 5. This question is in chapter 4. Do you think the author just want me to implement what I've learnt so far (until chapter 4: If Statements), or just in that of chapter 4, or no restriction ( implementing knowledge from other chapters as well) ?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, loops are basics of basics. You need them in order to repeat something.
    Any book that intends a student to use goto (or explicitly says so) is a sign of a bad book. So I'd say go ahead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    No the book didn't say anything about goto though. Got it from surfing internet, and didn't know about do while loop back then.

    But anyway, problem still isn't solved.

    I can't continue reading if it stays like this. Need to understand mistake asap...

    Again, the problem is that I can't input my username after retry password, and after putting the cin.ignore call after cin.getline, the username/password input become invalid even if it is actually valid during the retry. Please help.

    I suggest you try this program yourself. Because I'm afraid I gave you poor detail of the problem. Thank you.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    There. No goto now.

    Code:
      //Answer to page 71 Q 4, chapter 4: If Statements
    
    
    
    #include <iostream>;
    #include <string>;
    
    
    using namespace std;
    
    
    string username;
        string password;
    	string choice;
    
    
    
    
    
    
    
    
    
    
    int main()
    
    
    {   
    	
    	
    
    
    
    
    	
         do {
    	
    	 cout << "Username: ";
         getline(cin, username);
    	 cin.ignore();
    	 
    	 cout << "Password: ";
    	 getline(cin, password);
    	 cin.ignore();
    
    
    	
    
    
    
    
    
    
    	if ( username == "Johnny" && password == "x" || username == "Debb" && password == "y" || username == "Oh hai" && password == "z")
    	{ cout << "Valid password. You may now enter paradise.\n"; }
    
    
    	else 
    	{ cout << "Invalid username or password. Please try again.\n If you forgot your password, ask for help at nearby abyss's sinner support.\n\n";
          
         do {
    
    
    	  cout << "Retry? y/n \n\n";
    	  cin >> choice;   }
    
    
    	 while ( choice != "y" && choice != "n");
    	  
    	}
    	 }
    	     while ( choice == "y" && choice != "n");
    
    
    	
    	 
    	 
    	
    	 
    
    
        system("pause");
    	return 0;
    
    
    }
    Still can't validate username/password during retry. hmmm

  10. #10
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    I know how to handle this after reading through Chapter 5. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple username password program
    By Andr0id in forum C Programming
    Replies: 8
    Last Post: 08-17-2011, 10:14 AM
  2. Username & Password - Problem
    By Sedition X in forum C Programming
    Replies: 2
    Last Post: 04-08-2011, 06:57 AM
  3. Replies: 1
    Last Post: 07-15-2010, 09:15 AM
  4. going back to previous line
    By sankul in forum C Programming
    Replies: 5
    Last Post: 08-28-2007, 11:55 PM
  5. username password
    By jamie85 in forum C Programming
    Replies: 14
    Last Post: 03-14-2004, 05:46 PM

Tags for this Thread