The problem that i am working asks to calculate a customer's monthly bill. The customer has to input their name, which package they purchased, and how many hours were used. For example package a is for 9.95 a month 10 hours of access provided. Additional hours are 2.00 per hour. Im wondering if my syntax is correct...here's what i have so far...

``````#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
//Declare variables
const double PACKAGE_A_RATE = 9.95;
const double PACKAGE_B_RATE = 14.95;
const double PACKAGE_C_RATE = 19.95;

char packageA;
char packageB;
char packageC;
char packageName;
double packageACharge;
double packageBCharge;
double packageCCharge;
double hours;
double totalDue;
string customerName;

cout << "Enter your name: ";
cin >> customerName;

cout << "Enter the package you purchased: ";
cin >> packageName;

cout << "Enter the number of hours used: ";
cin >> hours;

if(hours ==10)
{
packageACharge = PACKAGE_A_RATE;
}
else if(hours > 10)
{
packageACharge = (packageACharge + hours) % 2;

cout << packageACharge<<endl;

}

system("pause");
return 0;
}``````

can anyone help?

everything was good, simple errors.. but your on your way to learning :)
2
Contributors
22
Replies
23
Views
9 Years
Discussion Span
Last Post by dvsConcept

if i did your math right then the problem was with your math (you used a % not a *) not with the if statement and i switched your package name to a string but you can switch it back if you like...hope this helps (it does compile now)

``````#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
//Declare variables
const double PACKAGE_A_RATE = 9.95;
const double PACKAGE_B_RATE = 14.95;
const double PACKAGE_C_RATE = 19.95;

char packageA = ' ';
char packageB = ' ';
char packageC = ' ';
string packageName = "";
double packageACharge = 0.0;
double packageBCharge = 0.0;
double packageCCharge = 0.0;
double hours = 0.0;
double totalDue = 0.0;
string customerName = "";

cout << "Enter your name: ";
cin >> customerName;

cout << "Enter the package you purchased: ";
cin >> packageName;

cout << "Enter the number of hours used: ";
cin >> hours;

if(hours ==10)
{
packageACharge = PACKAGE_A_RATE;
cout << packageACharge<<endl;
}
else if(hours > 10)
{
packageACharge = (packageACharge +((hours - 10) * 2))+10;

cout << packageACharge<<endl;

}

system("pause");
return 0;
}``````

by the way use code tags to display your code properly here...the other people might get mad if you dont! :)
[ code = c++ ] <---beginning tag
[ / code ] <--ending tag ((both with no spaces))

can i do the same format for packages b-c?

can i do the same format for packages b-c?

yes it will work just fine for b-c

For example, if the user uses 12 hours and the package calls for 9.95 per month 10 hrs of access, my answer on the calculator came out to be 13.95

For example, if the user uses 12 hours and the package calls for 9.95 per month 10 hrs of access, my answer on the calculator came out to be 13.95

ok let me change the way we are calculating because this is the answer you need right? one sec while i adjust the calculation

this will end with the answer you expected from your example (\$13.95)

``````#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
//Declare variables
const double PACKAGE_A_RATE = 9.95;
const double PACKAGE_B_RATE = 14.95;
const double PACKAGE_C_RATE = 19.95;

char packageA = ' ';
char packageB = ' ';
char packageC = ' ';
string packageName = "";
double packageACharge = 0.0;
double packageBCharge = 0.0;
double packageCCharge = 0.0;
double hours = 0.0;
double totalDue = 0.0;
string customerName = "";

cout << "Enter your name: ";
cin >> customerName;

cout << "Enter the package you purchased: ";
cin >> packageName;

cout << "Enter the number of hours used: ";
cin >> hours;

if(hours ==10)
{
packageACharge = PACKAGE_A_RATE;
cout << packageACharge<<endl;
}
else if(hours > 10)
{
//this turned out to be cleaner than the last
packageACharge = ((hours - 10) * 2) + PACKAGE_A_RATE;

cout << packageACharge<<endl;

}

if(hours ==10)
{
packageBCharge = PACKAGE_B_RATE;
cout << packageBCharge<<endl;
}
else if(hours > 10)
{

packageBCharge = ((hours - 10) * 2) + PACKAGE_B_RATE;

cout << packageBCharge<<endl;

}
system("pause");
return 0;
}``````

Two questions---
1) if i have a package that is 19.95 per month for unlimited access, how do figure that into the equation?

2) when im ready to output the bill, do i need to output it in every if statement?

Two questions---
1) if i have a package that is 19.95 per month for unlimited access, how do figure that into the equation?

2) when im ready to output the bill, do i need to output it in every if statement?

package C is the unlimited access price?

and you dont have to output in every if statement ill show you once you answer my question about package C

yes but it cant exceed 744 hours

also...quick question will every if statement be like this:

if(hours == 10)

else if(hours>10) ???

in order for you to determine which price to go with A,B,or C you are going to have to include some sort of comparison within your if(HERE) ill give you an example one sec.........

here's what i have but its not working

``````if(hours ==10)
{
packageACharge = PACKAGE_A_RATE;
cout <<packageACharge<<endl;
}
else if(hours > 10)
{
packageACharge = (packageACharge +((hours - 10) * 2)) +
PACKAGE_A_RATE;
cout << packageACharge<<endl;
}

else if(hours ==20)
{
packageBCharge = PACKAGE_B_RATE;
cout <<packageBCharge<<endl;
}
else if(hours > 20)
{
packageBCharge = (packageBCharge +((hours - 20) * 1)) +
PACKAGE_B_RATE;
cout << packageBCharge<<endl;
}

else if(hours <= 744)
{
packageCCharge = PACKAGE_C_RATE;
cout <<packageCCharge<<endl;
}
else if(hours <= 744)
{
packageCCharge = (packageCCharge +((hours - 744) * 2)) +
PACKAGE_C_RATE;
cout << packageACharge<<endl;
}``````

here this should work to your liking hopefully you understand why i implied the switch statement...if not ill explain it to you

``````#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
//Declare variables
const double PACKAGE_A_RATE = 9.95;
const double PACKAGE_B_RATE = 14.95;
const double PACKAGE_C_RATE = 19.95;

char packageA = ' ';
char packageB = ' ';
char packageC = ' ';
char packageName = ' ';
double hours = 0.0;
double totalDue = 0.0;
string customerName = "";
double packageCharge = 0.0;//replaced all of your packageA,B,CCharge variables for output purposes

cout << "Enter your name: ";
cin >> customerName;

cout << "Enter the package you purchased(a,b,or c): ";
cin >> packageName;

cout << "Enter the number of hours used: ";
cin >> hours;

switch(packageName)
//not sure if you went over switch/case yet but this is a good example of when to use one
//(also use them for menus)
{
case 'a'://package a chosen by user
if(hours == 10)
{
packageCharge = PACKAGE_A_RATE;

}
else if(hours > 10)
{
packageCharge = ((hours - 10) * 2) + PACKAGE_A_RATE;

}
break;
case 'b'://package b chosen by user
if(hours == 10)
{
packageCharge = PACKAGE_B_RATE;

}
else if(hours > 10)
{
packageCharge = ((hours - 10) * 2) + PACKAGE_B_RATE;

}
break;
case 'c'://package c chosen by user
if(hours == 10 && hours <= 744)
{
packageCharge = PACKAGE_C_RATE;

}
else if(hours > 744)
{
packageCharge = ((hours - 744) * 2) + PACKAGE_C_RATE;

}
break;
}
cout<<packageCharge;
system("pause");
return 0;
}``````

i used the switch/case to make the program determain which if statement to use depending on what package the user chose.

i see where you're going with it.....

package A--9.95 for 10 hours per month additonal hrs 2.00
package B--14.95 for 20 hours per month additonal hrs 1.00
package C was 19.95 with unlimited access but can't go over 744 hours...

so after the break i can display the user information right?

you could also add a do/while loop to lock the user out from picking any choice other than a,b,c for packageName like this:

``````#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
//Declare variables
const double PACKAGE_A_RATE = 9.95;
const double PACKAGE_B_RATE = 14.95;
const double PACKAGE_C_RATE = 19.95;

char packageA = ' ';
char packageB = ' ';
char packageC = ' ';
char packageName = ' ';
double hours = 0.0;
double totalDue = 0.0;
string customerName = "";
double packageCharge = 0.0;//replaced all of your packageA,B,CCharge variables for output purposes

system("cls");//clear screen
cout << "Enter your name: ";
cin >> customerName;
do
{
cout << "Enter the package you purchased(a,b,or c): ";
cin >> packageName;
}
while(packageName != 'a' && packageName != 'b' && packageName != 'c');//while packageName is not equal(!=) to a,b,or c

cout << "Enter the number of hours used: ";
cin >> hours;

switch(packageName)
{
case 'a':
if(hours == 10)
{
packageCharge = PACKAGE_A_RATE;

}
else if(hours > 10)
{
packageCharge = ((hours - 10) * 2) + PACKAGE_A_RATE;

}
break;
case 'b':
if(hours == 20)
{
packageCharge = PACKAGE_B_RATE;

}
else if(hours > 20)
{
packageCharge = ((hours - 20) * 2) + PACKAGE_B_RATE;

}
break;
case 'c':
if(hours == 10 && hours <= 744)
{
packageCharge = PACKAGE_C_RATE;

}
else if(hours > 744)
{
packageCharge = ((hours - 744) * 2) + PACKAGE_C_RATE;

}
break;
default:
cout<<"INVALID CHOICE"<<endl;
system("pause");
}

cout<<name<<' '<<packageName<<' '<<packageCharge;// display user information for bill
system("pause");
return 0;
}``````

i'll stick with the switch statement. so would the same formula for package A apply to packages b and C? because im getting squirly answers

Correct the break will take you completely out of the switch/case so than you can display user information for the bill such as:

``cout<<name<<' '<<packageName<<' '<<packageCharge;// display user information for bill``

no...the whole point of using the switch was to break out of getting all if statements working at the same time

if you use switch than it will only use the appropriate if statement for the order

OK THIS works perfectly

``````#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
//Declare variables
const double PACKAGE_A_RATE = 9.95;
const double PACKAGE_B_RATE = 14.95;
const double PACKAGE_C_RATE = 19.95;

char packageA = ' ';
char packageB = ' ';
char packageC = ' ';
char packageName = ' ';
double hours = 0.0;
double totalDue = 0.0;
string customerName = "";
double packageCharge = 0.0;//replaced all of your packageA,B,CCharge variables for output purposes

system("cls");//clear screen
cout << "Enter your name: ";
cin >> customerName;
do
{
cout << "Enter the package you purchased(a,b,or c): ";
cin >> packageName;
}
while(packageName != 'a' && packageName != 'b' && packageName != 'c');//while packageName is not equal(!=) to a,b,or c

cout << "Enter the number of hours used: ";
cin >> hours;

switch(packageName)
{
case 'a':
if(hours == 10)
{
packageCharge = PACKAGE_A_RATE;

}
else if(hours > 10)
{
packageCharge = ((hours - 10) * 2) + PACKAGE_A_RATE;

}
break;
case 'b':
if(hours == 20)
{
packageCharge = PACKAGE_B_RATE;

}
else if(hours > 20)
{
packageCharge = ((hours - 20) * 2) + PACKAGE_B_RATE;

}
break;
case 'c':
if(hours <= 744)
{
packageCharge = PACKAGE_C_RATE;

}
else if(hours > 744)
{
packageCharge = ((hours - 744) * 2) + PACKAGE_C_RATE;

}
break;
default:
cout<<"INVALID CHOICE"<<endl;
system("pause");
}

cout<<customerName<<' '<<packageName<<' '<<packageCharge;// display user information for bill
system("pause");
return 0;
}``````

hopefully this helps and you understand how everything works..

if you dont understand how everything is working than try and play with this debugging tool to watch what happens LINE FOR LINE

press f10 and than f10 to go to next line and so on..this way youll see what your program is doing line for line

thank you soooo much for your help. I really appreciate it

thank you soooo much for your help. I really appreciate it

no problem, anytime :)