wont read integer input correctly

i want it to tell me it is impossible to have more than 744 hours however if i input 744 or anything above it, it is just calculated as a price like the other 2 options. how do i code it so it says that it is too much?

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
  #include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Which package did you purchase?: A, B, or C?\n(A/B/C)\n\n";
    char pack;
    cin >> pack;
    switch (pack)
    {
    case 'a': cout << "\nyou picked A.\n\nHow many hours were you on the internet?:\n\n";
    float A_nethours;
    cin >> A_nethours;
    if (A_nethours <= 10)
    { cout << "\nyou have a bill of 9.95 in your account because you did not go over the limit.\n";
}
else if (A_nethours > 10)
{
     cout << "\nYou have a bill of " << (A_nethours - 10.00) * 2.00 + 9.95  << " in your account because of extra fees.\n";
     }
     else if (A_nethours > 744) // <-----problem here
     {
     cout << "\nThat is impossible.";
     }
     else
     {
         cout << "\nInvalid answer.";
         }
    break;
    case 'b': cout << "\n\nyou picked B.\n";
    break;
    case 'c': cout << "\n\nyou picked C.\n";
    break;
    default: cout << "Not a valid answer.";
}
cin.get();
cin.get();
cin.get();
}
    
Look at line 17
else if (A_nethours > 10)
a value of 744 is greater than 10, so that condition is true. The later test is thus ignored.

You need to re-arrange the code so that the tests are done in a different order.
remove the elses from your if statements and it will work.
closed account (D3pGNwbp)
I think I fixed your problem

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>
#include <string>
using namespace std;
int main()
{
    cout << "Which package did you purchase?: A, B, or C?\n(A/B/C)\n\n";
    char pack;
    cin >> pack;
    switch (pack)
    {
    case 'a': cout << "\nyou picked A.\n\nHow many hours were you on the internet?:\n\n";
    float A_nethours;
    cin >> A_nethours;
    if (A_nethours <= 10)
    { cout << "\nyou have a bill of 9.95 in your account because you did not go over the limit.\n";
}
else if (A_nethours > 10)
{
	// edited this area ----
	 if (A_nethours <= 744)
	 {
     cout << "\nYou have a bill of " << (A_nethours - 10.00) * 2.00 + 9.95  << " in your account because of extra fees.\n";
     }
     else if (A_nethours > 744) 
     {
     cout << "\nThat is impossible.";
     }
     else
     {
         cout << "\nInvalid answer.";
     }
	

}  // ----

    break;
    case 'b': cout << "\n\nyou picked B.\n";
    break;
    case 'c': cout << "\n\nyou picked C.\n";
    break;
    default: cout << "Not a valid answer.";
}
cin.ignore(); cin.get(); // also edited this(in place of the 3 cin.get()s)
}
Last edited on
Topic archived. No new replies allowed.