Problem with my loop and switch statement.

So I wrote this code and everything works fine but this one spot. When a user types in an in valid key and this only happens if it goes to the default statement the program will go into an infinite loop. Otherwise every other scenario works beautiful. I put a return 0; at the end of the default statement to stop this infinite loop and that worked but now it won't ask the user if they wish to continue. As you can guess this is for a class but the program did not require us to have loops I just added them because I thought it would make life easier.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
int main()
{

// Decalare variables.
    int number;
    float amountDue,
          hours;
    string input;

// Asks user the package subscription they have.
    cout <<"Select a subscription package:\n";
    cout <<"1.Package A \n";
    cout <<"2.Package B \n";
    cout <<"3.Package C \n";
    cout <<"4.Quit \n";
    cin >> number;

// Decalare value for loop
bool cont = true;
// Using while loop for the program to loop.
while (cont)
{
  // switch statement
switch (number)
{

    // Package A
    case 1:
    cout << "How many hours was used? ";
    cin >> hours;
        // if customer used less than 10 hours it will be a flat 9.95 charge.
        if(hours >= 0.00 && hours <= 10.00)
        {
        amountDue = 9.95;
        cout << "The total amount due is $" << amountDue << endl;
        }
            // if custiomr used more than 10 hours it will be a charge of 9.95 plus 2 for each addtional hour.
            else if(hours > 10.00 && hours <= 744.00)
            {
            amountDue = 9.95 + 2.00*( hours - 10);
            cout << "The total amount due is $" << amountDue << endl;
            }
                //if user typed in an invalid number.
                else if(hours > 744.00 || hours < 0)
                {
                cout << "The hours used must be between 0.00 and 744.00." << endl;
                }
                break;

        //Package B
        case 2 :
        cout << "How many hours was used? ";
        cin >> hours;
            if(hours >= 0.00 && hours <= 20.00)
            {
            // if the customer used less than 20 hours a flat 14.95 charge.
            amountDue = 14.95;
            cout << "The total amount due is $" << amountDue << endl;
            }
                // if the sutomer used more than 20 hours a charge of 14.95 plus 1 for each addtional hour.
                else if(hours > 20.00 && hours <= 744.00)
                {
                amountDue = 14.95 + 1.00*( hours - 20);
                cout << "The total amount due is $" << amountDue << endl;
                }
                    //if user typed in an invalid number.
                    else if(hours <0.00 || hours > 744.00)
                    {
                    cout << "The hours used must be between 0.00 and 744.00." << endl;
                    }
                    break;

    //Package C
    case 3 :
    cout << "How many hours was used? ";
    cin >> hours;
        // if user types any valid number a flat charge of 19.95.
        if(hours >= 0.00 && hours <= 744.00)
        {
        amountDue = 19.95;
        cout << "The total amount due is $" << amountDue << endl;
        }
            //if user typed in an invalid number.
            else if(hours <0.00 || hours > 744.00)
            {
            cout << "The hours used must be between 0.00 and 744.00." << endl;
            }
            break;

        // quiting program
        case 4 :cout << "Press enter to quit program.";
        break;

        // catch all other input
        /* Note: I could not get my loop to work with default and it is only in this case.
        I tried removing the return 0; but it would just go into an infinite loop I did
        several hours of resreach and could not find out why.maybe give me a hint? or possible
        have an answer as to why*/
        default: cout <<"The valid choices are 1 through 4.\n";
                 cout <<"Run the program again and select one of those.";
        return 0;

}

    //Asks users if they wish to continue using the program.
    cout << "Do you wish to continue? (Press Y/N)" << endl;
    cin >> input;
    if(input == "Y" || input == "y")
    {
    // Asks user the package subscription they have.
    cout <<"Select a subscription package:\n";
    cout <<"1.Package A \n";
    cout <<"2.Package B \n";
    cout <<"3.Package C \n";
    cout <<"4.Quit \n";
    cin >> number;
    }

        else {cont=false;}
        {

        }

}
    cout <<"\n Press Enter to quit."<<endl;
    return 0;

}
Last edited on
If a read operation fails cin will be enter an error state where all future read operations will fail. You can check if an operation failed by putting it inside an if statement. To leave the error state you can use the clear function. You will also have to remove the invalid input from cin because it's still there. You can do that by using ignore.
1
2
3
4
5
if (cin >> number)
{
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Last edited on
Okay I am confused on where should I put this? should it be inside of my default? and is there a header I need to use for it?


If a read operation fails cin will be enter an error state where all future read operations will fail. You can check if an operation failed by putting it inside an if statement. To leave the error state you can use the clear function. You will also have to remove the invalid input from cin because it's still there. You can do that by using ignore.
1
2
3
4
5
if (cin >> number)
{
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
}



Note that it should be if (!(cin >> number))

Okay I am confused on where should I put this?


You should do it whenever you ask someone to enter a number:

Instead of:
1
2
3
4
double db;
cout << "Please enter a number: ";
cin >> db;
cout << "You entered: " << db << ".  Thanks for playing\n";


You want:
1
2
3
4
5
6
7
8
9
10
int db;
cout << "Please enter a number: ";
if (!(cin >> db))
{
  cin.clear();
  cin.ignore(80, '\n');
  cout << "Hey, I asked for a number\n";
}
else
  cout << "You entered: " << db << ".  Thanks for playing\n";
Thank you that solved my issue gosh it was bothering me so much lol
Topic archived. No new replies allowed.