Sign in to follow this  
whitin

Switch Statement : Solved (some advice?)

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 choosing

char again = 'y'; //--- restart define

switch (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;
}





Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this