| | |
having problem with an if statement...
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 57
Reputation:
Solved Threads: 0
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;
}•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
here this should work to your liking hopefully you understand why i implied the switch statement...if not ill explain it to you
i used the switch/case to make the program determain which if statement to use depending on what package the user chose.
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 = ' '; 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.
Last edited by dvsConcept; Feb 22nd, 2009 at 1:33 am.
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
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:
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 = ' '; 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; }
Last edited by dvsConcept; Feb 22nd, 2009 at 1:45 am.
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
Correct the break will take you completely out of the switch/case so than you can display user information for the bill such as:
c++ Syntax (Toggle Plain Text)
cout<<name<<' '<<packageName<<' '<<packageCharge;// display user information for bill
•
•
Join Date: Jan 2009
Posts: 39
Reputation:
Solved Threads: 0
OK THIS works perfectly
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 = ' '; 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; }
![]() |
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.
Views: 670 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





