Char Detection

closed account (N6fwAqkS)
Hi there! I Apologize for any inconvenience, however I have a question of which I have been unable to solve. In the code below, I would like to include a case within the switch command, in which when a character is entered, instead of an integer, an error message, along the lines of "Did not understand, please re-enter" is displayed. Unfortunately I have not been able to achieve this. I believed that if I were to put:
1
2
3
case (char)bill1
 cout << "Did not understand, please re-enter";
 continue;


The problem would be fixed. Unfortunatley 'Continue' statements are not accepted in a Switch statement, and casting bill1 as a char would not work either. Any ideas?

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  cout << "\n\nWelcome to the Bill Payer Application! \n\n" << "Please enter the grand sum of money that can be allocated to paying your bills this cycle. \n\n" << "Grandsum: $";
            cin >> grandsum;
            cout << "\n\nYou have " << grandsum << " dollars to allocate towards bills this cycle.\n\nPlease enter your first bill price and press enter. \n\n[!]Type 0 and press enter in any of the fields to immediatley exit.[!]\n\n";
            cout << "Bill 1 price: $";
            cin >> bill1;
            billgrand = bill1;
            int bill1;
            switch (bill1)
            {
                case 0:
                    goto billgrand;
                    break;
                default:
                    int bill2;
                    cout << "Bill 2 price: $";
                    cin >> bill2;
                    billgrand = bill1+bill2;
                    switch (bill2)
                {
                    case 0:
                        goto billgrand;
                        break;
                    default:
                        int bill3;
                        cout << "Bill 3 price: $";
                        cin >> bill3;
                        billgrand = bill1+bill2+bill3;
                        switch (bill3)
                    {
                        case 0:
                            goto billgrand;
                            break;
                        default:
                            int bill4;
                            cout << "Bill 4 price: $";
                            cin >> bill4;
                            billgrand = bill1+bill2+bill3+bill4;
                            switch (bill4)
                        {
                            case 0:
                                goto billgrand;
                                break;
                            default:
                                int bill5;
                                cout << "Bill 5 price: $";
                                cin >> bill5;
                                billgrand = bill1+bill2+bill3+bill4+bill5;
                                switch (bill5)
                            {
                                case 0:
                                    goto billgrand;
                                    break;
                                default:
                                    int bill6;
                                    cout << "Bill 6 price: $";
                                    cin >> bill6;
                                    billgrand = bill1+bill2+bill1+bill4+bill5+bill6;
                                    switch (bill6)
                                {
                                    case 0:
                                        goto billgrand;
                                        break;
                                    default:
                                        int bill7;
                                        cout << "Bill 7 price: $";
                                        cin >> bill7;
                                        billgrand = bill1+bill2+bill1+bill4+bill5+bill6+bill7;
                                        switch (bill7)
                                    {
                                        case 0:
                                            goto billgrand;
                                            break;
                                        default:
                                            int bill8;
                                            cout << "Bill 8 price: $";
                                            cin >> bill8;
                                            billgrand = bill1+bill2+bill1+bill4+bill5+bill6+bill7+bill8;
                                            switch (bill8)
                                            {
                                                case 0:
                                                    goto billgrand;
                                                    break;
                                                default:
                                                    int bill9;
                                                    cout << "Bill 9 price: $";
                                                    cin >> bill9;
                                                    billgrand = bill1+bill2+bill1+bill4+bill5+bill6+bill7+bill8+bill9;
                                                    switch (bill9)
                                                {
                                                    case 0:
                                                        goto billgrand;
                                                        break;
                                                    default:
                                                        int bill10;
                                                        cout << "Bill 10 price: $";
                                                        cin >> bill10;
                                                        billgrand = bill1+bill2+bill1+bill4+bill5+bill6+bill7+bill8+bill9+bill10;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
             }

I understand the code is most likely unnecessarily bulky and incredibly inefficient, however I am only 14 and very new to the language of C++. If you have any questions or need clarification please do not refrain from asking. Again, thank you.
the default is a case of its own not to be included in other cases. And should handle input that does not match one of the other cases

1
2
3
4
5
6
7
switch (variable)
{
	case 1: { do stuff break; }
	case 2: {do other stuff break; }
	default: {take care of other entries break; }
}


Its a rough example but that is what I think you are after.
Last edited on
closed account (N6fwAqkS)
Thank You CodeGoggles! However, I understand that, but if I'm using 0 as an exit number, and all other numbers are to be accepted by the default case, how am I to make a case that would return an error message if a character, instead of an integer, is to be entered?
Last edited on
You would use the default case to do that like

1
2
3
4
5

switch (bill1)

 default : { cout << "invalid entry";


I am sure you can do what you want but the current structure of your switch is not how switch's are meant to be used. each case is supposed to have its own value. Your cases are also inside the previous case. And that will invalidate the switch altogether.

I want to help you so if you could post your entire code particularity what's happening with billgrand. I would be ablel to edit it and comment the areas I changed for your clarification.
Last edited on
Topic archived. No new replies allowed.