Thread: Another Noob Question

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    16

    Another Noob Question

    Is it possible to do a case within a case? For example:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int y;
        int x;
        int a;
        int b;
        cout << "Selection:\n";
        cout << "1. Start\n";
        cout << "2. LoL\n";
        cout << "3. Quit\n";
        cout << "Please make your selection: ";
        cin >> y;
    switch (y) {
      case 1:
           switch (x) {
                  case 1:
                       break;
                  case 2:
                       break;
                  case 3:
                       break;
                  case 4:
                       break;
                  default:
                       cout << "Invalid input.\n";
                       break;
                       }
                       
        break;
      case 2:
        cout << "\nWrong Button!\n\n";
        break;
      case 3:
        return 0;
        break;
      default:
        cout << "\nInvalid input.n\n";
      }
        system("PAUSE");
    }
    I have actual things filled in, in the sub-cases, but it doesn't prompt me with a 2nd selection. It just skips to the "Invalid Input" portion

    Edit: Hope I indented it right. Feel free to yell at me if I didn't.

    Edit Edit: A simple yes or no would suffice; however, if no a suggested alternative method would be extremely appreciated.
    Last edited by Nightwarrior; 10-12-2007 at 08:53 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Nightwarrior
    I have actual things filled in, in the sub-cases, but it doesn't prompt me with a 2nd selection. It just skips to the "Invalid Input" portion
    Where is x ever initialized? You get something put into y from the user but never x.
    Code:
    int x;
        int a;
        int b;
        cout << "Selection:\n";
        cout << "1. Start\n";
        cout << "2. LoL\n";
        cout << "3. Quit\n";
        cout << "Please make your selection: ";
        cin >> y;
    switch (y) {
      case 1:
           switch (x) {
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    I have the:

    Code:
    int y;
    int x;
    int a;
    int b;
    Code:
    switch (x) {
    Was in my OP as well. Doesn't let me do a 2nd input though.

    (BTW, if I'm completely missing your point, feel free to slap me. )

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I think you are missing his point.

    Your switch case x will always execute your defualt label; because the value of x was never defined, it's normally automaticly zero, and as you can see, you have no case 0: in that switch.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Quote Originally Posted by Yarin View Post
    I think you are missing his point.

    Your switch case x will always execute your defualt label; because the value of x was never defined, it's normally automaticly zero, and as you can see, you have no case 0: in that switch.
    Ah. I understand now. Thanks!

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Hrm, what if I wanted to output 2 numbers divided by each other? Like real division. 5 / 2 = 2.5. Dev C++ says 5 / 2 = 2.

    It's only doing how many times the number can go in it wholey, and I'd like decimals.

    I'm using

    Code:
    cout << a / b << "\n";

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    type int only allows whole numbers, eg ...,-3,-2,-1,0,1,2,3,... you will want to use float or double:

    Code:
    double math;
    math = 5/2;
    
    cout << math;
    Output is: 2.5
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Thanks a lot!

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    One last question. Is it possible for it to loop back to the start?

    i.e.

    Switch y case 1 starts the program
    switch x case 1 does division

    it stops after you do the division. Can you make it go back to the start of switch x so you could do a 2nd problem with it? Something along the lines of:

    Code:
    cout << "Would you like to do another problem?";
    Thanks in advance.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    16
    Guess it'd help if I included the code.

    Code:
    #include <cstdlib> 
    #include <iostream> 
    
    using namespace std; 
    
    int main() 
    { 
        int y; 
        int x; 
        double a; 
        double b; 
        cout << "Selection:\n"; 
        cout << "1. Start\n"; 
        cout << "2. LoL\n"; 
        cout << "3. Quit\n"; 
        cout << "Please make your selection: "; 
        cin >> y; 
    switch (y) { 
      case 1: 
           cout << "***NOTE*** Only actions with 2 numbers total is supported. Format will be a + b; a - b; a / b; a * b;\n"; 
           cout << "Selection:\n"; 
           cout << "1. Addition\n"; 
           cout << "2. Substraction\n"; 
           cout << "3. Division\n"; 
           cout << "4. Multiplication\n"; 
           cout << "Please choose your action: "; 
           cin >> x; 
           switch (x) { 
                  case 1: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a + b << "\n";
                       break; 
                  case 2: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a - b << "\n"; 
                       break; 
                  case 3: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a / b << "\n"; 
                       break; 
                  case 4: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a * b << "\n"; 
                       break; 
                  default: 
                       cout << "Invalid input!\n"; 
                       break; 
                       } 
                        
        break; 
      case 2: 
        cout << "\nWrong Button! =)\n\n"; 
        break; 
      case 3: 
        return 0; 
        break; 
      default: 
        cout << "\nInvalid input!\n\n"; 
      } 
        system("PAUSE"); 
    }

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    This is a bit sloppy because the tabs are all messed up so it may not have everything on the right line, but I think this is what you want.

    Code:
    #include <cstdlib> 
    #include <iostream> 
    
    using namespace std; 
    
    int main() 
    {
        int input = 0;
        int y; 
        int x; 
        double a; 
        double b;
        while ( input == 0 ) {
        cout << "Selection:\n"; 
        cout << "1. Start\n"; 
        cout << "2. LoL\n"; 
        cout << "3. Quit\n"; 
        cout << "Please make your selection: "; 
        cin >> y;
    switch (y) { 
      case 1: 
           cout << "***NOTE*** Only actions with 2 numbers total is supported. Format will be a + b; a - b; a / b; a * b;\n"; 
           cout << "Selection:\n"; 
           cout << "1. Addition\n"; 
           cout << "2. Substraction\n"; 
           cout << "3. Division\n"; 
           cout << "4. Multiplication\n"; 
           cout << "Please choose your action: "; 
           cin >> x; 
           switch (x) { 
                  case 1: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a + b << "\n";
                       break; 
                  case 2: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a - b << "\n"; 
                       break; 
                  case 3: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a / b << "\n"; 
                       break; 
                  case 4: 
                       cout << "Please enter your first number: "; 
                       cin >> a; 
                       cout << "Please enter your second number: "; 
                       cin >> b; 
                       cout << "\n"; 
                       cout << a * b << "\n"; 
                       break; 
                  default: 
                       cout << "Invalid input!\n"; 
                       break; 
                       } 
                        
        break; 
      case 2: 
        cout << "\nWrong Button! =)\n\n"; 
        break; 
      case 3: 
        return 0;
        input = 1;
        break; 
      default: 
        cout << "\nInvalid input!\n\n"; 
      } 
      }
        system("PAUSE"); 
    }
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM