help with switch

This is a discussion on help with switch within the C++ Programming forums, part of the General Programming Boards category; I've been working on some simple programs using the switch statement for c++ programming class and am always having the ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    help with switch

    I've been working on some simple programs using the switch statement for c++ programming class and am always having the same problem with nested switches. I am trying to create a menu like system in the console to give the users choices.



    Code:
    		
    char mc;
    char ac;
    cout << "which choice do you want? (a)lgebra, (g)eometry?";
    cin >> mc;
    switch (mc)
    {
    case 'a':
    	cout << "what operation you want to perform? a(d)dition, d(i)vision, (s)ubtraction?";
    		cin >> ac;
    		switch (ac)
    		{
    		case 'd':
    			cout << "addition"<<endl;
    			break;
    		case 'i':
    			cout << "division"<<endl;
    			break;
    		case 's':
    			cout << "subtraction"<<endl;
    			break;
    		}
    		case 'g':
    		cout << "geometry";
    		break;
    }
    return 0;
    }
    Obviously, it will first prompt you for a choice between algebra or geometry. If you choose algebra, it gives you another set of choices. The problem is that once you choose between the nested algebra choices, the program also goes ahead and prints out "geometry" from case 'g' no matter what you choose. How can I make the program not do this and simply return to the first menu?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You forgot to add a break statement after the 'a' case. switch statements have default fallthrough, which means if you don't break out of the switch then all of the cases after the selected case will be executed until the end of the statement or a break.
    Code:
    switch (mc)
    {
    case 'a':
      cout << "what operation you want to perform? a(d)dition, d(i)vision, (s)ubtraction?";
      cin >> ac;
      switch (ac)
      {
      case 'd':
        cout << "addition"<<endl;
        break;
      case 'i':
        cout << "division"<<endl;
        break;
      case 's':
        cout << "subtraction"<<endl;
        break;
      }
      break;
    case 'g':
      cout << "geometry";
      break;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    You are amazing btw But why would it execute case 'g' if g was not the value of that variable anyway?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    switch(mc)
    {
    case 'a':
    case 'g':
    
    
    }
    Would be basically the same as saying:
    Code:
    if (mc=='a'||mc=='g')
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21