Switch Statment Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huzzaa
    New Member
    • Apr 2007
    • 9

    Switch Statment Problems

    I am using a switch statement that will decide from a random number what message to display.

    I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to display a certain message.

    here is my code

    Code:
     
    
    srand(time(0));
    
    int messageSelect = 1+rand()%100;
    
    switch (messageSelect)
    {
     case ..........  : cout << "I'm sorry but you lose. Try again!" << endl << endl;
    					break;
     case .......... : cout << "You don't guess well on tests do you?" << endl;
    		       cout << endl;
    					break;
     case .......... : cout << "Bad luck I guess. Try again!" << endl << endl;
    					break;
     case .......... : cout << "The Greeks may think the gods don't favor you.";
    		       cout << " Try again!" << endl << endl;
    					break;
     case .......... : cout << "You have yet to find the magic number.";
    		    cout << " Try again!" << endl << endl;
    					break;
     case ..........: cout << "Wrong number. Try again!" << endl << endl;
    					break;
     case .......... : cout << "Guess what! Try again!" << endl << endl;
    					break;
     case .......... : cout << "Looks to me like you're gonna have to try again!";
    		     cout << endl << endl;
    					break;
     case ..........: cout << "Doesn't look to be in your favor.";
    		     cout << " Try again!" << endl << endl;
    					break;
     case ..........: cout << "Your outcome looks bleak. Try again!";
    		      cout << endl << endl;
    					break;
    }

    after the case statment I need something that does basically the function of
    Example:

    case < 10 :

    case > 10 && < 20 :

    case > 20 && < 30 :


    and so on untill 100.

    I am not familiar with the syntax to preform that opeation.
    I tried storing those operations into variables and just placing the variables after case but the same error occured.

    this is the error for the first case:
    error C2059: syntax error : '<'

    And this is the error for all the following cases:
    error C2059: syntax error : '>'


    I assume that I can't start the syntax with either the > or <. Do I need to enclose it in parenthesis?


    I know in an If statment I could word it like this

    Code:
    if (messageSelect < 10)
      Statement....
    else if (messageSelect is > 10 && < 20)
      Statement....
    and so on, but that is alot of code.

    Can I impliment those operations into a switch statement, or do I have to use an If statement?

    If I can use a switch, how would I do so?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by huzzaa
    I am using a switch statement that will decide from a random number what message to display.

    I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to display a certain message.

    here is my code

    Code:
     
    
    srand(time(0));
    
    int messageSelect = 1+rand()%100;
    
    switch (messageSelect)
    {
     case ..........  : cout << "I'm sorry but you lose. Try again!" << endl << endl;
    					break;
     case .......... : cout << "You don't guess well on tests do you?" << endl;
    		       cout << endl;
    					break;
     case .......... : cout << "Bad luck I guess. Try again!" << endl << endl;
    					break;
     case .......... : cout << "The Greeks may think the gods don't favor you.";
    		       cout << " Try again!" << endl << endl;
    					break;
     case .......... : cout << "You have yet to find the magic number.";
    		    cout << " Try again!" << endl << endl;
    					break;
     case ..........: cout << "Wrong number. Try again!" << endl << endl;
    					break;
     case .......... : cout << "Guess what! Try again!" << endl << endl;
    					break;
     case .......... : cout << "Looks to me like you're gonna have to try again!";
    		     cout << endl << endl;
    					break;
     case ..........: cout << "Doesn't look to be in your favor.";
    		     cout << " Try again!" << endl << endl;
    					break;
     case ..........: cout << "Your outcome looks bleak. Try again!";
    		      cout << endl << endl;
    					break;
    }

    after the case statment I need something that does basically the function of
    Example:

    case < 10 :

    case > 10 && < 20 :

    case > 20 && < 30 :


    and so on untill 100.

    I am not familiar with the syntax to preform that opeation.
    I tried storing those operations into variables and just placing the variables after case but the same error occured.

    this is the error for the first case:
    error C2059: syntax error : '<'

    And this is the error for all the following cases:
    error C2059: syntax error : '>'


    I assume that I can't start the syntax with either the > or <. Do I need to enclose it in parenthesis?


    I know in an If statment I could word it like this

    [CODE}
    if (messageSelect < 10)
    Statement....
    else if (messageSelect is > 10 && < 20)
    Statement....
    [/CODE]

    and so on, but that is alot of code.

    Can I impliment those operations into a switch statement, or do I have to use an If statement?

    If I can use a switch, how would I do so?
    You can use a switch. You can divide the random number by ten and use that for the case statements.

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by huzzaa
      I am using a switch statement that will decide from a random number what message to display.

      I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to display a certain message.

      here is my code

      Code:
       
      
      srand(time(0));
      
      int messageSelect = 1+rand()%100;
      
      switch (messageSelect)
      {
       case ..........  : cout << "I'm sorry but you lose. Try again!" << endl << endl;
      					break;
       case .......... : cout << "You don't guess well on tests do you?" << endl;
      		       cout << endl;
      					break;
       case .......... : cout << "Bad luck I guess. Try again!" << endl << endl;
      					break;
       case .......... : cout << "The Greeks may think the gods don't favor you.";
      		       cout << " Try again!" << endl << endl;
      					break;
       case .......... : cout << "You have yet to find the magic number.";
      		    cout << " Try again!" << endl << endl;
      					break;
       case ..........: cout << "Wrong number. Try again!" << endl << endl;
      					break;
       case .......... : cout << "Guess what! Try again!" << endl << endl;
      					break;
       case .......... : cout << "Looks to me like you're gonna have to try again!";
      		     cout << endl << endl;
      					break;
       case ..........: cout << "Doesn't look to be in your favor.";
      		     cout << " Try again!" << endl << endl;
      					break;
       case ..........: cout << "Your outcome looks bleak. Try again!";
      		      cout << endl << endl;
      					break;
      }

      after the case statment I need something that does basically the function of
      Example:

      case < 10 :

      case > 10 && < 20 :

      case > 20 && < 30 :


      and so on untill 100.

      I am not familiar with the syntax to preform that opeation.
      I tried storing those operations into variables and just placing the variables after case but the same error occured.

      this is the error for the first case:
      error C2059: syntax error : '<'

      And this is the error for all the following cases:
      error C2059: syntax error : '>'


      I assume that I can't start the syntax with either the > or <. Do I need to enclose it in parenthesis?


      I know in an If statment I could word it like this

      [CODE}
      if (messageSelect < 10)
      Statement....
      else if (messageSelect is > 10 && < 20)
      Statement....
      [/CODE]

      and so on, but that is alot of code.

      Can I impliment those operations into a switch statement, or do I have to use an If statement?

      If I can use a switch, how would I do so?
      Case doesn't support < or > so you must use it like:

      case 1:case2:case 3:case5:....... ............... .

      case11:case12:. ......

      and so on.

      So it will be simpler to use if-else,or if u insist of using switch divide number with ten.
      Last edited by Savage; Apr 19 '07, 09:20 PM. Reason: forgot something

      Comment

      • huzzaa
        New Member
        • Apr 2007
        • 9

        #4
        Originally posted by Savage
        Case doesn't support < or > so you must use it like:

        case 1:case2:case 3:case5:....... ............... .

        case11:case12:. ......

        and so on.

        So it will be simpler to use if-else,or if u insist of using switch divide number with ten.

        I used if-else. Thanks so much for your reply!

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by huzzaa
          I used if-else. Thanks so much for your reply!
          Allways a pleasure to help!!


          Savage

          Comment

          Working...