One Last Question Regarding "Switch Statements"

My code is nearly perfect with exception of two issues: 1) When 1, 2, or 3 is selected in the second switch statement, the correct doctor's name displays (i.e, "Making an appointment with Dr. Green.") But, to get out of the program, you have to select a character or a number other than 2 or 3 and then press enter to get the message, "Press any key to continue..." 2) I also have a problem with the "Press any other key to talk to an operator" not working correctly in the first switch statement (after a 1, 2, or 3 is entered). I've been working on this program all day and it's driving me crazy (errrrrrr), so any assistance correcting these two problems would be appreciated. :-)

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>

using namespace std; 

int main ()
{
   int choice = 0;
   int drChoice = 0;
   int choice1 = 1;
   int choice2 = 2;
   int choice3 = 3;
   
   cout << "Enter your choice [1, 2, 3]: ";
   cin >> choice;
    
    switch (choice)
    {
       case 1:  cout << "Press 1 for making an appointment with Dr. Green" << endl;                       
                cout << "Press 2 for making an appointment with Dr. Fox" << endl;
                cout << "Press 3 for making an appointment with Dr. Davis" << endl;
                cout << "Press any other key to talk to an operator" << endl;
                cin >> drChoice;
                break;                          
       case 2:  cout << "Billing questions" << endl;
                break;     
       case 3:  cout << "Talking to a nurse" << endl;
                break;       
       default: cout << "Talking to an operator" << endl;
    }    
     
    switch (drChoice)
    {
       case 1:  cout << "Making an appointment with Dr. Green..." << endl; 
                cin >> choice1;
                break;  
       case 2:  cout << "Making an appointment with Dr. Fox..." << endl;
                cin >> choice2; 
                break;  
       case 3:  cout << "Making an appointment with Dr. Davis..." << endl;
                cin >> choice3;
                break;   
    }        
                
    system ("pause");
    return 0;
}
Last edited on
For the first problem try removing the cin don't know why you put it on the first place...

For the second move/copy the default: cout << "Talking to an operator" << endl; to the second switch.
I was advised by another commenter (freddy92) to put it there (cin >> drChoice;), and if I remove it, it screws the whole program up. Also moving/copying the "default: cout << "Talking to an operator" << endl;" to the second switch doesn't resolve the problem I'm having, but I appreciate your input.
I created your program today & made some changes to it. I moved lines 31 to 42 between lines 22 & 33. Put the default case back in the drChoice switch.

I don't know why you have the cin with Choice1, Choice2 & Choice3 now. I would remove them from the program as you don't need them.

The program should work now. It does for me unless I'm missing something. Good luck.
Last edited on
My bad the cin I meant were the ones in the second switch statements

Could you explain me when do you want the "talk to an operator" to happen?
Hi leftcoast,

Could you please post the program you created? I tried to follow your instructions (move the lines to where you said, etc.) but the program's coming out all screwy (eyes are buggin' out of my head after 10 hours of trying to fix this program). Hope you don't mind...
Here's the code that I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

int main()
{
    int choice = 0;
    int drchoice = 0;

    cout << "Press 1 for doctor." << endl;
    cout << "Press 2 for billing questions." << endl;
    cout << "Press 3 to transfer to nurse." << endl;
    cout << "Press 0 to transfer to operator." << endl;
    cout << "\nEnter your choice: ";
    cin >> choice;

    switch(choice)
    {
        case 1 : cout << "\nPlease select your doctor." << endl;
        cout << "Press 1 for Dr. Green." << endl;
        cout << "Press 2 for Dr. Fox." << endl;
        cout << "Press 3 for Dr. Davis." << endl;
        cout <<"\nMy doctor selection is: ";
        cin >> drchoice;

            switch(drchoice)
            {
                case 1: cout << "\nMake appointment with Dr. Green." << endl;
                break;

                case 2: cout << "\nMake appointment with Dr. Fox." << endl;
                break;

                case 3: cout << "\nMake appointment with Dr. Davis." << endl;
                break;

                default : cout << "\nTransfer to operator." << endl;
            }
        break;

        case 2 : cout << "\nBilling questions?" << endl;
        break;

        case 3 : cout << "\nTransfer to nurse?" << endl;
        break;

        default : cout << "\nTransfer to operator." << endl;
    }

    return 0;
}
Thank you so much, leftcoast! Problem is, we have yet to learn about "\n", so I'm sure my professor would bust me on that. However, I wish I COULD use your program because the output is so much neater. I assume I'll learn more about using "\n" in the not-too-distant future. However I will try using some of your code to see it it makes a difference in output -- Meanwhile (back at the bug-eyed ranch), I'm going to retire for the evening but will be posting again tomorrow. Uggghhhhhh....

Thanks again... :-)
Hi ccsdude,

My apologies for not being clear, but I can provide you with a more detailed explanation tomorrow.

Thank you for your time. :-)
You're welcome Tate! \n is for creating a new line. Yes, it makes it easier on the eyes when reading the outputs. I don't know why you couldn't use \n when you are using switch coding syntax at this point but I'm not the one taking your class. You didn't mentioned if my programs works on your computer? Have a good evening!
After a good night's rest my eyeballs have retracted back into their sockets. I made a few changes in the program this morning, and it now works as it should. Yeah!!

I want to thank all the folks who took the time out of their busy schedules to assist. This forum is a godsend... Thank you so much!!
Topic archived. No new replies allowed.