| | |
having problem with an if statement...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
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...
can anyone help?
#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?
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
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)
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))
c++ Syntax (Toggle Plain Text)
#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))
Last edited by dvsConcept; Feb 22nd, 2009 at 12:40 am.
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
ok let me change the way we are calculating because this is the answer you need right? one sec while i adjust the calculation
Last edited by dvsConcept; Feb 22nd, 2009 at 12:47 am.
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
this will end with the answer you expected from your example ($13.95)
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by dvsConcept; Feb 22nd, 2009 at 1:05 am.
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 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?
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
Last edited by dvsConcept; Feb 22nd, 2009 at 1:13 am.
![]() |
Similar Threads
- Implementation problem (C++)
- Problem on IF STATEMENT on TRIGGER with MySql (MySQL)
- Problem in using Map (C++)
- simple 'if statement' issue (C#)
- Number guessing game problem (C++)
- Question: Linear Time Sorting Problem (Computer Science)
- problem with "for" statement (C++)
- TMT Pascal Input Console problem (Pascal and Delphi)
- gcd problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Character array
- Next Thread: program with padding a string.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





