I have to write a program that allows the user 5 chances to guess a random value. This random value changes 7 times and each time i have to allow the user 5 attempts to guess these values. My main issue is that the nested for loop is not executing 7 times. Instead when the program runs, it only allows the user 5 attempts at the first guess value, THEN it just automatically closes. Not sure why, would be awesome if someone could point out my error(s). Thanks very much in advance.
PS : Pay no mind to the code commented out, its just stuff i was fooling around with.
// Programmed by Anthony Altreb. Date 10-16-12
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main ()
{
int magic;// magic number.
int guess;// user's guess.
int c=0;// the amount of right guesses
magic=rand()%20+1;//get a random number. between 0-19 then adds one.
/*char name;*/
/* int flag=0; */
// This is the for loop that will control how many magic number values you have to guess
/* cout<<"Please enter your name \n";
cin>>name;
cout<<" Hello\n "<< name <<" you have 5 chances to guess the right value for 7 DIFFERENT numbers.";
*/
for (int guesschance=1; guesschance >= 8;guesschance++);
{
// This is the for loop that will control the number of chances you get to guess the value.
magic=rand()%20+1;
for (int guessnum=1; guessnum < 6; guessnum++)
{
cout<<"Please enter guess #" <<guessnum<<" for guess value "<<guesschance<<": "; // The prompt message.
// Obtain a guess value from user.
cin>>guess;
if (guess==magic)
{
// Prompt message that tells user that their guess value was correct.
cout<<"\n***You are well built, my respects!!!***\nMagic Number was"<<' '<<magic<<"!\n";
// Prompt message that tells the user at which attempt the user guessed correctly.
cout<<"Your guess was the #"<< guessnum <<" attempt.\n";
// The expression is rechecked and found to be false thus breaking out of this block of code and ends the program.
guessnum=6; /* && flag=1*/
}
else
{
cout<<"\n...Sorry, you are wrong, please try again! \n"; // prompt message that lets user know their guess value was incorrect.
if (guess>magic) cout<<"\nYour approximation has exceeded my expectation. Please lower your guess value.\n"; // prompt message that lets user know their guess value was too high
else cout<<"\nYour approximation is far lower then I expected. Please raise the value of your guess.\n"<<endl; // prompt message that lets user know their guess value was too low.
}
if (guessnum==5 && magic!=guess)
{
cout<<"The magic number was "<<magic<<endl;
}
}
}
}
{
cout<<"Your success rate was "<< (c/7)*100<<"%"<<endl;
}
return 0; // end of program.
}
for (int guesschance=1; guesschance >= 8;guesschance++);
Should be for (int guesschance=1; guesschance < 8;guesschance++)
The semi-colon shouldn't be there, so I removed it, plus you're checking for when guesschance has been used 7 times, hence the 1 to less than 8. After this, we can check if there are other small errors. Good luck..