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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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().

  2. #2
    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.

  3. #3
    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) ?

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