Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    c++ progress programme.

    Hey please can you help me
    basically im learning c++ and i admit im crap at it.
    anyway this is just a program which i makeing while i learn c++
    its has a menu of all games and examples of c++ ive done
    but theres a login and if the user enters a wrong pass
    i want them to havethe option to go back to the login.
    Code:
    // c++ program to demostrate my progress in c++ programming.
    #include <iostream> // for simple input and output.
    #include <string> // for strings
    #include <cstdlib> // for rand();
    #include <cmath> // for math functions
    #include <cstdio> // for gets() function instead of cin >>.
    #include <cctype> // for text functions(toupper().
    
    using namespace std;
    
    // function prototype.
    void login();
    void switchf();
    void guessg();
    void login()
    {
    	 
    	 // user's input.
    	 char str[80];
    	 char strg[80];
    	 
    	 // compared with correct user and pass.
    	 string user = "user";
    	 string pass = "pass";
    	 
    	 // redirect
    	 string direct; // user's input.
    	 string yes = "y";
    	 string no = "n";
    	 
    	 cout << "User: ";
    	 gets(str);
    	 cout << "Password: ";
    	 gets(strg); cout << endl;
    	 
    	 // checks the input if it matches.
    	 if(str==user && strg==pass)
    	 {
    				  cout << "Welcome.. " << endl;
    				  switchf();
    				  }
    				  else
    				  {
    					  cout << "User or Password is wrong " << endl;
    					  cout << "Do you want to try again (y/n) ? " << endl;
    					  cin >> direct;
    					  }
    					 
    					 }
    void guessg()
    {
    	int guess;
    	int num = rand();
    	
    	cout << "Welcome to The Guess game " << endl;
    	
    	do
    	{
    			cout << "Enter your guess " << endl;
    			cin >> guess;
    			if(guess == num)
    			{
    					 cout << "Correct " << endl;
    					 cout << "The randomly generated number was " << num << endl;
    					 cout << "Do you want to return to he menu (y/n) ? " << endl;
    					 cin.get();
    					 }
    					 else
    					 {
    							 cout << "You are wrong " << endl;
    							 if(guess > num)
    							 {
    									  cout << "Your guess was to high. " << endl;
    									  }
    									  else
    									  {
    											  cout << "Your guess is to low " << endl;
    											  }
    											  }
    											  } while(guess != num);
    }
    void switchf()
    {
    	 // Menu.
    	 int i;
    	 cout << "These are a list of small games or examples of c++ programes. " << endl;
    	 cout << "1. Guess game(text based game) " << endl;
    	 cin >> i;
    	 
    	 switch(i)
    	 {
    			  case 1:
    				   guessg();
    							break;
    			  default:
    				   cout << "Incorrect request " << endl;
    							break;
    							}
    }   
    				   
    	 
    	 
    int main()
    {
    	login();
    	cin.get();
    }
    thats what ve done so far
    please look at this area
    Code:
    }
    				  else
    				  {
    					  cout << "User or Password is wrong " << endl;
    					  cout << "Do you want to try again (y/n) ? " << endl;
    					  cin >> direct;
    					  }
    thanks

  • #2
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can create a do while loop:

    //variable with the answer
    do
    {
    // try if password is correct.
    // ask if the user want to try another time.
    }while (answer == Yes); //If is correct the loop executes.
    //If is not correct continues here.
    //In the while test, you can put another condition that ends it when the password is corret:
    while ((answer == Yes) || (password == correct));

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, the second while must see like:

    while((answer == yes && (password != correct));

    or something like this.


  •  

    Posting Permissions

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