hello how can i do a for loop .. for a guessing game... and the only 5 oportunities to answer..... forexample no you wrong u have 4 guess left"

heres my code !!1

this one just keeps giving me an unfishing loop grrrr!!!

``````#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;

void welcome()
{
cout << " welcome to my game i will pick a number between 1 - 50"
<< "and you will try to guess it :)"<< endl;

}

int main()
{

welcome();

int secret_number;

int guess;

bool number;

//4. Seed the pseudo-random generator with the statement:
srand( static_cast<int>( time( NULL ) ) );

secret_number = rand() % 50 + 1;
cout << " please enter a number" << endl;
cin >> guess;

for (int i=0; i<5; i--)
{
int times =5;

{

if (guess== secret_number)
{
cout << " yay you got it"
<< " thanks for playing" <<endl;

}
else if (guess > secret_number)&&(times == i--)
{
cout << " nope guess is too high"
<< " you have" <<  times<< "left" << endl;
}
else
cout << " you got no more tries :( " << endl;
}
}

return 0;
}``````

``````for (int i=0; i<5; i--) // you are decrementing i, it will become -1
{
int times =5; // times will always be five``````

Is this really what you want to do?

``else if (guess > secret_number)&&(times == i--)``

A) You have no condition for guess being less than the random number
B) You are decrementing i, this -will- affect your loop
C) Times will always be 5, so in your std::cout, you will always tell the user they have 5 guesses left.

Perhaps this is what you wanted:

``````int times(5);
for(i=0;i<5;i++) {
--times;
std::cout << "You have " << times << " guesses remaining." << std::endl;
if( guess == secret_number ) {
std::cout << "Correct." << std::endl;
return 0; // exit program
} elseif( guess < secret_number ) {
std::cout << "Your guess was too low." << std::endl;
} elseif( guess > secret_number ) {
std::cout << "Your guess was too high." << std::endl;
}
}
return 0;``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.