Sign in to follow this  
Kalner

Switch and If/else C++

Recommended Posts

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?[code]#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");[/code]

This is 1/3 of the whole program.

Share this post


Link to post
Share on other sites
Here it is.

[code]#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 = 0;
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{
cout << lo.getLoss() << number << endl;
system("PAUSE");
}
break;
case 'm':
case 'M':
number = 1+(rand()%25);
cout << "Guess a number beetween 1 and 25" << endl;
cin >> gn;
if(gn == number){
cout << wo.getWin()<<endl;
system("PAUSE");
}
else{
cout << lo.getLoss() << number << endl;
system("PAUSE");
}
case 'd':
case 'D':
number = 1+(rand()%50);
cout << "Guess a number beetween 1 and 50" << endl;
cin >> gn;
if(gn == number){
cout << wo.getWin()<< endl;
system("PAUSE");
}
else{
cout << lo.getLoss() << number << endl;
system("PAUSE");
}
default:
number = 1+(rand()%100);
cout << "Fine I made it hard for you."
cout << "Guess a number beetween 1 and 100!"
cin >> gn;
if(gn == number){
cout << wo.getWin() << endl;
system("PAUSE");
}
else{
cout << lo.getLoss() << number << endl;
system("PAUSE");
}

}
}[/code]

Share this post


Link to post
Share on other sites
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][color=#666600]<<[/color][color=#000000] [/color][color=#008800]"Fine I made it hard for you."[/color]
[color=#000000]cout [/color][color=#666600]<<[/color][color=#000000] [/color][color=#008800]"Guess a number beetween 1 and 100![/color]


Both of these lines are missing semi-colon terminators.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
When you have it working, consider breaking the actual game logic into a separate function. The main routine will call this with the current difficulty. It might look something like this:
[code]
void gameLogic(int max)
{
// ...
}


int main()
{
srand(time(0));

cout << "Welcome to Guess The Number 3.0" << endl;
cout << "Please choose your difficluty. E = Easy M= Medium H = Hard" << endl;

char dif;
cin >> dif;

switch(dif)
{
case 'e':
case 'E':
gameLogic(10);
break;
case 'm':
case 'M':
gameLogic(25);
break;
case 'd':
case 'D':
gameLogic(50);
break;
default:
cout << "Fine I made it hard for you."
gameLogic(100);
break;
}

// Pause goes here, if necessary
}

[/code]

Share this post


Link to post
Share on other sites
As a sidenote, if you decide to create local variables inside a case-statement, you need to enclose this in a local scope. At least GCC complains about this because it has to know the scope of the variable (jump to case label [-fpermissive]. crosses initialization of 'int a').
For instance, this will produce the error:
[code]
switch (cin.get())
{
case 'E':
int a = 'E';
std::cout << a << std::endl;
break;

case 'B':
int b = 'B';
std::cout << b << std::endl;
break;
}
[/code]

Which can be resolved using:
[code]
switch (cin.get())
{
case 'E':
{
int a = 'E';
std::cout << a << std::endl;
}
break;

case 'B':
{
int b = 'B';
std::cout << b << std::endl;
}
break;
}
[/code]

Share this post


Link to post
Share on other sites
Thanks alot guys, especially nife87 and rip-off, you guys got me to fix it then mkae the whole think smaller. =D my first game is complete

EDIT: forgot to mention you Jebbles =D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this