Jump to content
  • Advertisement
Sign in to follow this  
Kalner

Switch and If/else C++

This topic is 2492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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?#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");


This is 1/3 of the whole program.

Share this post


Link to post
Share on other sites
Advertisement
Here it is.

#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");
}

}
}

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=#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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!