Switch Statement : Solved (some advice?)

Started by
3 comments, last by Kylotan 18 years, 11 months ago
Are you able to put a while statement into one of the cases of a switch statement? Im making my "random number game" more complex by adding difficulty levels but I'm not entirely sure how I implement this. I will post my code but I would rather just have an explination not the answer. Any advice will help. Thanks.

#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
   cout << "Welcome to the random number game. By: Austin Overton" << endl;
    cout << "Difficulty:\n\n";
    cout << "1 = Easy Peasy (0-5)\n";
    cout << "2 = Normal Warmal(0-25)\n";
    cout << "3 = Crazy Insane(0-100)\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:

char again = 'y';
  while(again == 'y')
      {
int num1 = 0;

      srand(time(NULL));
      int num2 = rand()%6;

        cout << "Please pick a number between 0 and 5." << endl;

        cout << "Number... ";
        cin >> num1;

      if (num1 != num2)
          {
          cout << "Sorry you lose. :( The correct number was " << num2 << endl << endl;
          cout << "Do you want to play again? (y/n): ";
          cin >> again;
          }
      else
          {
          cout << "Congratulations you guessed correctly!" << endl << endl;
          cout << "Do you want to play again? (y/n): ";
          cin >> again;
          }
}
    cout << "Ok, toddles!" << endl;

    system("pause");
  return 0;
}



[Edited by - whitin on May 28, 2005 9:26:11 PM]
------Signature------Inspiring young coder/programmer!
Advertisement
Yes you can place a while statement as a case in a switch statement.

Quote:Original post by Anonymous Poster
Yes you can place a while statement as a case in a switch statement.


Thank you very much. I have accomplished something very basic. I random number game where you can pick the difficulties. I am quite proud of this little achivement. Now I would like to know what can I do to improve it... maybe how to make it cleaner? or little features I can add? Anything would be great.

#include <cstdlib>#include <iostream>#include <ctime>using namespace std;int main(){  cout << "Welcome to the random number game. By: Austin Overton" << endl;    cout << "Difficulty:\n\n";         //--- Start difficulty choosing      cout << "1 = Easy Peasy (0-5)\n";      cout << "2 = Normal Warmal(0-25)\n";      cout << "3 = Crazy Insane(0-100)\n\n";int choice;  cout << "Choice: ";  cin >> choice;             //---- End difficulty choosingchar again = 'y';          //--- restart defineswitch (choice){case 1:                      //---- Easy-----------  while(again == 'y')      {int num1 = 0;      srand(time(NULL));              //-----Rand-------      int num2 = rand()%6;            //---Function------        cout << "Please pick a number between 0 and 5." << endl;        cout << "Number... ";        cin >> num1;      if (num1 != num2)          {          cout << "Sorry you lose. :( The correct number was " << num2 << endl << endl;          cout << "Do you want to play again? (y/n): ";          cin >> again;          }      else          {          cout << "Congratulations you guessed correctly!" << endl << endl;          cout << "Do you want to play again? (y/n): ";          cin >> again;          }}    cout << "Ok, toddles!" << endl;    break;case 2:                   //-----Normal----------  while(again == 'y')      {int num1 = 0;                                          //------Rand------      srand(time(NULL));                  //-----Function---      int num2 = rand()%26;        cout << "Please pick a number between 0 and 25." << endl;        cout << "Number... ";        cin >> num1;      if (num1 != num2)          {          cout << "Sorry you lose. :( The correct number was " << num2 << endl << endl;          cout << "Do you want to play again? (y/n): ";          cin >> again;          }      else          {          cout << "Congratulations you guessed correctly!" << endl << endl;          cout << "Do you want to play again? (y/n): ";          cin >> again;          }}    cout << "Ok, toddles!" << endl;    break;case 3:                        //------Hard------------  while(again == 'y')      {int num1 = 0;      srand(time(NULL));                      //----Rand-----      int num2 = rand()%101;                  //---Function--        cout << "Please pick a number between 0 and 100." << endl;        cout << "Number... ";        cin >> num1;      if (num1 != num2)          {          cout << "Sorry you lose. :( The correct number was " << num2 << endl << endl;          cout << "Do you want to play again? (y/n): ";          cin >> again;          }      else          {          cout << "Congratulations you guessed correctly!" << endl << endl;          cout << "Do you want to play again? (y/n): ";          cin >> again;          }}    cout << "Ok, toddles!" << endl;    break;default:        cout << "Please pick legit choice." << endl;}    system("pause");  return 0;}




------Signature------Inspiring young coder/programmer!
Yeah, you should be able to put a while into a switch case, although you might want to put it around the switch. Also, you might want to use a bool for again instead of a char.

One last thing - the comments in your code are a bit self-explainatory, and probably don't need to be there.
You should also put srand() at the beginning of int main() rather than at several points during the program. srand is an initialisation function that resets the random number generator each time you call it. Therefore it should just be done once, at the start. :)

This topic is closed to new replies.

Advertisement