If statement ignored/ Selection structure failure

I need to write a program that computes a telephone plan bill according to the plan selected. There are three plans to choose from. My program will function properly for plan A, but for plan B and C, it does not calculate and only prints plan As price.

#include <iostream>
#include <string>

using namespace std;

int main()
{
// declare constants (strings) and variables
string Name;
string Adress;
string Tel;
string Month;
char plan; // defines the package type
double C1 = 19.95;// defines the total cost
double C2 = 34.95;
double C3 = 59.95;
int t; // defines the number of minutes used per month


// prompt to input name and display
cout << "Enter full name (First_Last): ";
cin >> Name;

// prompt for mailing adress
cout << "Enter mailing adress (sub '_' as space): ";
cin >> Adress;

// prompt for telephone number
cout << "Enter telephone number: ";
cin >> Tel;

// prompt for month
cout << "Enter month: ";
cin >> Month;

// prompt for package
cout << "Enter plan package (A, B, C): ";
cin >> plan;

// prompt for number of minutes used
cout << "Enter the number of minutes used this month: ";
cin >> t;

// Prints user information
cout << "\nName: " << Name << endl;
cout << "\nMailing adress: " << Adress << endl;
cout << "\nTel: " << Tel << endl;
cout << "\nMonth: " << Month << endl;
cout << "\nPlan: " << plan << endl;
cout << "\nMinutes used: " << t << endl;

// computes amount due
if (plan == 'A')
cout << "\nPlan A price: $" << C1;
{
while ((t > 200) && (plan != 'B') && (plan != 'C'))
{
C1 = C1 + (0.08*(t-200));
break;
}
cout << "\nYour balance due for plan A this month is: $" << C1 << endl;
}
system("pause");
return 0;


if (plan == 'B')
cout << "\nPlan B price: $" << C2;
{
while (t > 500)
{
C2 = C2 + (0.06*(t-500));
break;
}
cout << "\nYour balance due for plan B this month is: $" << C2 << endl;
}
system("pause");
return 0;

if ((plan == 'C') && (t >= 0) && (plan != 'A') && (plan != 'B'))
{
cout << "\nPlan C price: $" << C3;
cout << "\nPlan C is Unlimited. \nYour balance due this month is: $" << C3 << endl;
}
system("pause");
return 0;

system("pause");
return 0;
}
Last edited on
Format your code using code tags in the future.

On lines 64, 78, 86, and 89 you have return statements. As soon as execution gets to the return on line 64, main ends and your program closes. Remove the return statements on lines 64 and 78. Also, lines 88 and 89 are not necessary.
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    // declare constants (strings) and variables
    string Name;
    string Adress;
    string Tel;
    string Month;
    char plan; // defines the package type
    double C1 = 19.95;// defines the total cost
    double C2 = 34.95;
    double C3 = 59.95;
    int t; // defines the number of minutes used per month


    // prompt to input name and display
    cout << "Enter full name (First_Last): ";
    cin >> Name;

    // prompt for mailing adress
    cout << "Enter mailing adress (sub '_' as space): ";
    cin >> Adress;

    // prompt for telephone number
    cout << "Enter telephone number: ";
    cin >> Tel;

    // prompt for month
    cout << "Enter month: ";
    cin >> Month;

    // prompt for package
    cout << "Enter plan package (A, B, C): ";
    cin >> plan;

    // prompt for number of minutes used
    cout << "Enter the number of minutes used this month: ";
    cin >> t;

    // Prints user information
    cout << "\nName: " << Name << endl;
    cout << "\nMailing adress: " << Adress << endl;
    cout << "\nTel: " << Tel << endl;
    cout << "\nMonth: " << Month << endl;
    cout << "\nPlan: " << plan << endl;
    cout << "\nMinutes used: " << t << endl;

    // computes amount due
    if (plan == 'A')
        cout << "\nPlan A price: $" << C1;
    {
        while ((t > 200) && (plan != 'B') && (plan != 'C'))
        {
            C1 = C1 + (0.08*(t-200));
            break;
        }
        cout << "\nYour balance due for plan A this month is: $" << C1 << endl;
    }
    system("pause");
    return 0;


    if (plan == 'B')
        cout << "\nPlan B price: $" << C2;
    {
        while (t > 500)
        {
            C2 = C2 + (0.06*(t-500));
            break;
        }
        cout << "\nYour balance due for plan B this month is: $" << C2 << endl;
    }
    system("pause");
    return 0;

    if ((plan == 'C') && (t >= 0) && (plan != 'A') && (plan != 'B'))
    {
        cout << "\nPlan C price: $" << C3;
        cout << "\nPlan C is Unlimited. \nYour balance due this month is: $" << C3 << endl;
    }
    system("pause");
    return 0;

    system("pause");
    return 0;
} 



Also, your if blocks are structured incorrectly for plans A and B. You should have the opening brace immediately after the condition, not a cout.
This...
1
2
3
4
    if (plan == 'B')
        cout << "\nPlan B price: $" << C2;
    {
        while (t > 500) //... 

becomes
1
2
3
4
    if (plan == 'B')
    {
        cout << "\nPlan B price: $" << C2;
        while (t > 500)  //... 
Last edited on
Thank you. It worked
Topic archived. No new replies allowed.