Question:
What do I need to do to make this pro gramme work.?
anonymous
2011-09-22 12:44:02 UTC
I'm making a small text/number based game using C++. I have not fully studied how to make it and so have guessed, I have two errors, and it is the same error in the two function.

(error: name lookup of "x" changed for ISO "for" Scoping)

#include
#include
#include
using namespace std;
int fguess();
int sguess();

int main()
{
fguess();

cin.get();
return 0;
}

int fguess(){

int number;
cout << "Pick a number between 1 and 6 and lets see how you do." << endl;
cin >> number;

srand(time(0));

for (int x= 1; x<=1;x++){
cout << 1 + (rand()%6) << endl;
}

if(number = x){
cout << "Congratulations you guessed the right number!" << endl;
cout << "You won, have another go!" << endl;
sguess();
}

else{
cout <<"You get one more go!" << endl;
sguess();
}
}

int sguess(){

int number;
cout << "Pick a number between 1 and 6 and lets see how you do." << endl;
cin >> number;

srand(time(0));

for (int x= 1; x<=1;x++){
cout << 1 + (rand()%6) << endl;
}

if(number = x){
cout << "Congratulations you guessed the right number!" << endl;
cout << "Have another go!" << endl;
}

else{
cout << "Sorry you didn't win today" << endl;
return 0;
}

}


I'm sure you can figure out what my aim is.

Thanks for the help.
Three answers:
anonymous
2011-09-22 12:45:12 UTC
Fallout Boy!
repp4radu
2011-09-22 12:58:04 UTC
The problem is that practically your int x is not defined. you wrote:

for (int x= 1; x<=1;x++){

cout << 1 + (rand()%6) << endl;

}



if(number = x)



but by declaring x inside your for it means it is only considered to be available there, and it is immediately deleted after exiting for.

To solve the problem declare it where you have also declared "number".



Good luck!
chingonsonson
2011-09-22 13:22:29 UTC
repp4radu is right your x is declared inside of the for loop, you have to declare it outside of it maybe in the top of your function.



But there's also another problem in your conditional if

you have :



if( number = x)



that will assign the value of x into the variable number and what i think you want is to compare whether it is equal to x, so you have to put == like this:



if(number == x)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...