...

View Full Version : c++ progress programme.



Mr.popo
09-07-2007, 08:31 PM
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.


// 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

}
else
{
cout << "User or Password is wrong " << endl;
cout << "Do you want to try again (y/n) ? " << endl;
cin >> direct;
}


thanks

UnTio
09-10-2007, 06:11 PM
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));

UnTio
09-10-2007, 06:24 PM
Sorry, the second while must see like:

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

or something like this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum