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 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;
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.
#include <iostream>
#include <string>
usingnamespace 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) //...