Jump to content

  • Log In with Google      Sign In   
  • Create Account


Switch and If/else C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Kalner   Members   -  Reputation: 120

Like
0Likes
Like

Posted 19 February 2012 - 09:12 AM

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.

Sponsor:

#2 BSt   Members   -  Reputation: 102

Like
1Likes
Like

Posted 19 February 2012 - 09:54 AM

Let's show us more code.
(( I am learning English. ))

#3 Kalner   Members   -  Reputation: 120

Like
0Likes
Like

Posted 19 February 2012 - 10:00 AM

hmm?
What do you mean?

#4 BSt   Members   -  Reputation: 102

Like
1Likes
Like

Posted 19 February 2012 - 10:07 AM

The everything in switch statement.
(( I am learning English. ))

#5 Kalner   Members   -  Reputation: 120

Like
0Likes
Like

Posted 19 February 2012 - 10:13 AM

ah ok.. lemme get to you on that in a few minutes

#6 Kalner   Members   -  Reputation: 120

Like
0Likes
Like

Posted 19 February 2012 - 10:21 AM

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

	    }
}


#7 BSt   Members   -  Reputation: 102

Like
3Likes
Like

Posted 19 February 2012 - 10:35 AM

You didn't put
break;
in case 'M' and 'D', if i am right.
(( I am learning English. ))

#8 Serapth   Crossbones+   -  Reputation: 5262

Like
1Likes
Like

Posted 19 February 2012 - 10:40 AM

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.


cout << "Fine I made it hard for you."
cout << "Guess a number beetween 1 and 100!


Both of these lines are missing semi-colon terminators.

#9 Kalner   Members   -  Reputation: 120

Like
0Likes
Like

Posted 19 February 2012 - 10:47 AM

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.

#10 Jebbs   Members   -  Reputation: 276

Like
0Likes
Like

Posted 20 February 2012 - 02:29 AM

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.

#11 rip-off   Moderators   -  Reputation: 8066

Like
0Likes
Like

Posted 20 February 2012 - 06:58 AM

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:
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
}



#12 nife87   Members   -  Reputation: 516

Like
0Likes
Like

Posted 20 February 2012 - 11:27 AM

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:
	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;
	}

Which can be resolved using:
    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;
    }


#13 Kalner   Members   -  Reputation: 120

Like
0Likes
Like

Posted 20 February 2012 - 03:26 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS