So, yesterday I started making a geuss the number game and there was a problem that if you geussed it right it would show a loss.I tried today of using classes for losses instead of a bunch of code in the if and else curly brackets. Now even with class's I still get an error. Is there some kind of prblem with my if statement or is there a problem when an if statement is in a switch?#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
class Win{
public:
void setWin(string x){
win = x;
}
string getWin(){
return win;
}
private:
string win;
};
class Loss{
public:
void setLoss(string z){
loss = z;
}
string getLoss(){
return loss;
}
private:
string loss;
};
int main()
{
int number;
char gn;
char dif;
srand(time(0));
Win wo;
wo.setWin("You Won!");
Loss lo;
lo.setLoss("You Lost, the number was ");
cout << "Welcome to Guess The Number 3.0" << endl;
cout << "Please choose your difficluty. E = Easy M= Medium H = Hard" << endl;
cin >> dif;
switch(dif){
case 'e':
case 'E':
number = 1+(rand()%10);
cout << "Guess a number beetween 1 and 10" << endl;
cin >> gn;
if(gn == number){
cout << wo.getWin() <<endl;
system("PAUSE");
}
else{
lo.getLoss() << number << endl;
system("PAUSE");
It would be helpful if you gave us the actual error messages.
That said, in addition to the logic error BSt pointed out ( your two missing break statements ), you have a syntax error.
[color=#000000]cout [color=#666600]<<[color=#000000] [color=#008800]"Fine I made it hard for you."
[color=#000000]cout [color=#666600]<<[color=#000000] [color=#008800]"Guess a number beetween 1 and 100!
Both of these lines are missing semi-colon terminators.
sorry I was rushing to get it finished but I get no error messages when I put break points after 'M' and 'D' also when I put semi colons. Th eproblem is that the if statement completely doesnt work.
Example:
I choose E as difficulty
I press 6 as my geuss
Than if I am right it says "You Lost" " The number was 6" when I chose 6
and if I choose 7 it will proerly display 6 beacuse I got it wrong.
It took me a bit to get what was going on here, but after some hard looking I know why it always says you lost.
Your gn variable is a character and your "number" variable is an integer. When you guess the number 6, your gn variable has a value of 54, which is the character '6'. When you compare the two, it compares the numarical values. Since 54 != 6, it thinks you guessed wrong. Change your gn from a char to an int and it will let you guess the right answer.