Results 1 to 4 of 4

Thread: Guess my number game

  1. #1

    Default Guess my number game

    Please help me know what I am doing wrong in this simple Guess my number game code. First of all, I wrote the code using an if and a couple of else statement and it worked properly. But I then swaped it to use switch/case statement and it will not see out the results. Instead, it will only print out the first two lines of code but not the result of the test

    Code:
    // SIMPLE GUESS MY NUMBER GAME.
    # include "stdafx.h"
    # include <iostream>
    using namespace std;
    
    int main(void)
    {
    	
    	cout << "Guess my number game" "\n";
    	cout << "Guessed Number:" "\n";
    	int MyNumber = 53;
    	int GuessedNumber = 62;
    	switch (MyNumber) 
    	{
    		case 1:
    	
    		if (GuessedNumber < MyNumber)
    			cout<<"Wrong Number. Go higher" "\n";
    		break;
    		case 2:
    	
    		if (GuessedNumber == MyNumber)
    			cout << "Congratulations. You have won" "\n";
    		break;
    		case 3:
    	 
    		 if (GuessedNumber > MyNumber)
    			cout << "Wrong Number. Go lower" "\n";
    		 break;
    	}
    	
    	cin.get();
    }
    Last edited by MattB; 07-02-2010 at 11:01 AM. Reason: please use [CODE] tags :)

  2. #2

    Default

    in the swictch /case statement your using MyNumber and MyNumber equals 53. so in your case 1 statement it's asking 'if MyNumber equals 1 then do this (first if statement), if MyNumber equals 2 then do this (second if statement). so as MyNumber is 53 it never passes any of the case statements.

    you don't need the switch case statement, just let the if statements run through (without the 'break's).

    EDIT: if you really want to use the switch case to 'test out using one' then get the if statements to set a variable to 1, 2 or 3 and use the switch statement on the new variable to decide what gets outputed to the screen......just a suggestion.
    Last edited by Smoothnshiny; 07-02-2010 at 10:51 AM.

  3. #3

    Default

    The switch statement will never run.

    When you use a switch statement, you are comparing your "switching" variable to your "cases". You are declaring your switch variable to be "MyNumber". So the program will only enter case 1 if "MyNumber" = 1. Case 2 will only execute if "MyNumber"=2 and so on. Since you have stated your "MyNumber" is 53, you would expect the program to execute case 53. but that line doesn't exist, so it will never do anything.


    "Time fades even legend..."
    -Kain


    Si hoc legere scis numium eruditionis habes.
    A truly wise man never plays leapfrog with a Unicorn.
    Artificial intelligence is no match for natural stupidity.
    Never argue with an idiot. They drag you down to their level then beat you with experience.

  4. #4

    Default

    Thank you guys for your quick replies. So basically in this instance, it is not ideal to use switch/ case statement and I should just stick with the if and couple of else statements that worked fine. Thanks my people

Similar Threads

  1. Wrong number
    By Steveox90 in forum Newbies Area
    Replies: 11
    Last Post: 12-14-2009, 08:12 PM
  2. Guess that's me then...
    By Nyao_Chan in forum Newbies Area
    Replies: 5
    Last Post: 08-17-2009, 08:49 AM
  3. [Game] Simple Number Guessing Input Driven Event Loop
    By Entertainer in forum T2G Students
    Replies: 3
    Last Post: 05-19-2009, 10:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •