need help with this assignment. im having trouble with if and else. I enter package A and then it ask me for the hours twice. just run it if you dont get me. here are a few examples of how running the program should look, please help me!:S

example 1


Customer name: Ben Sanders
Which package was purchased? A
How many hours were used? 700

Customer name: Goofus Gallant
Package A
The total amount due is $ 1389.95

Ex 2


Customer name: Ikant Count
Which package was purchased? B
How many hours were used? 678

Customer name: blah blah
Package B
The total amount due is $ 672.95


ex 3


Customer name: help me
Which package was purchased? C

Customer name: Igot Money
Package C
The total amount due is $ 19.95

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	int hours; 
	double cost;
	string name;
	char package;

	cout <<"Enter customer name:";
	getline(cin, name);

	
	cout <<"select the following package:" << endl;
	cout <<"Package A $9.95 per month, 10 hours of access, additional hours are $2.00 per hour" << endl;
	cout <<"Package B $14.95 per month, 20 hours of access, additional hours are $1.00 per hour" << endl;
	cout <<"Package C $19.95 per month, unlimited hours" << endl;


	cout <<"Enter your choice:";
	cin >> package;

	if (package == 'A' || 'a')
		cout <<"Enter hours used:";
		cin >> hours;

		if (hours >= 10)
			(9.95 + (hours - 10) * 2);
		else 
		cost = 9.95;

	if (package == 'B' || 'b')
		cout <<"Enter hours used:";
		cin >> hours;
		
		if (hours >= 20)
			(14.95 + (hours - 20) * 2);
		
		else cost = 14.95;

	if (package == 'C' || 'c')
		cout <<"19.95:" << endl;

	cout << cost;


	


	system("pause");
	return 0;
}

Recommended Answers

All 17 Replies

Change lines like this:

if (package == 'A' || 'a')

to:

if (package == 'A' || package == 'a')

Edit:

You have some bracket problems. Do you mean this?

if (package == 'B' || package == 'b')
       {
		cout <<"Enter hours used:";
		cin >> hours;
		
		if (hours >= 20)
			(14.95 + (hours - 20) * 2);
		
		else cost = 14.95;
       }

Note the brackets.

i did and still doesnt run properly =[

i did and still doesnt run properly =[

Correct. There are still other errors in addition to the ones I pointed out. Give it a shot and if you can't get it, post your updated code and a new question.

thanks. and yeah i updated it with the brackets. and now cost is the problem. it says "Run-Time Check Failure #3 - The variable 'cost' is being used without being initialized." hmmmmm i guess ill do some more reading and try to correct it but if i cant hopefully i get some more help.

thanks. and yeah i updated it with the brackets. and now cost is the problem. it says "Run-Time Check Failure #3 - The variable 'cost' is being used without being initialized." hmmmmm i guess ill do some more reading and try to correct it but if i cant hopefully i get some more help.

Here's a debugging strategy. Run the program six times, twice for each package. For each package, run the program where you enter one hour and have a run where you enter 100 hours. That should cover all six of your if options. Now, do they ALL give that error? Does your program run correctly under any of them? If it does, focus on the option that gave you good results and look at the corresponding code. Look at the options that don't run well and look at that code and compare and see if there is a difference. If so, change that code so it is more like the code that runs correctly, then try again and see if it fixes the problem.

thanks. and yeah i updated it with the brackets. and now cost is the problem. it says "Run-Time Check Failure #3 - The variable 'cost' is being used without being initialized." hmmmmm i guess ill do some more reading and try to correct it but if i cant hopefully i get some more help.

Well, initialized the variable "cost" then. In fact, make it a habit to initialize all your variables.

well i ran it a few times for wach package and cost is still the issue. same problem.
idk what the heck can it be.

well i ran it a few times for wach package and cost is still the issue. same problem.
idk what the heck can it be.

We can't help without seeing your code. If you made the corrections I suggested correctly, your program will run correctly under certain circumstances and incorrectly under others. We can't do anything with this post because you haven't posted the revised code, nor have you fully explained the current behavior. All we can do is give you the full working program, which defeats the purpose of the forum. Post the revised code and a more detailed description of the problem please.

Post your code and also, did you do like Vern did and by initializing cost set it equal to 0 or anything when you declare it?

so i got it to work heres my updated code

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	int hours; 
	double cost;
	string name;
	char package;
	cost = 0;

	cout <<"Enter customer name:";
	getline(cin, name);

	
	cout <<"select the following package:" << endl;
	cout <<"Package A $9.95 per month, 10 hours of access, additional hours are $2.00 per hour" << endl;
	cout <<"Package B $14.95 per month, 20 hours of access, additional hours are $1.00 per hour" << endl;
	cout <<"Package C $19.95 per month, unlimited hours" << endl;


	cout <<"Enter your choice:";
	cin >> package;

	if (package == 'A' || package == 'a')
	{
		cout <<"Enter hours used:";
		cin >> hours;

		if (hours > 10)
			cost = (9.95 + (hours - 10) * 2);
		else 
		cost = 9.95;
			cout <<"Customer name:" << name << endl;
			cout <<"Package:" << package << endl;
			cout <<"Your total amount due is:" << '$' << cost << endl;
	}


	if (package == 'B' || package == 'b')
	{
		cout <<"Enter hours used:";
		cin >> hours;
		
		if (hours > 20)
			cost = (14.95 + (hours - 10) * 2);
		
		else cost = 14.95;
				cout <<"Customer name:" << name << endl;
				cout <<"Package:" << package << endl;
				cout <<"Your total amount due is:" << '$' << cost << endl;
	}

	if (package == 'C' || package == 'c')
		
	{
		cout <<"Customer name:" << name << endl;
		cout <<"Package:" << package << endl;
		cout <<"Your total amount due is $19.95:" << endl;
		
	}


	

	


	system("pause");
	return 0;
}

im almost finished. i need to put if the user enters an invalid package letter like (D, E, X, P)
i know you put something like
if (package != 'A' || package != 'a' || package != 'B' || package != 'b' || package != 'C' || package != 'c')

cout <<"invalid package" ;

will that work? and if so do i need to put it in every section for package a, package b, package c?

also the same thing with hours if it exceed 744 need to put invalid i know the code but would it get confuse since i already have if (hours >10) etc... since that calculates the total amount due.

and last thing if the user would hav saved money purchasing a diff package but im not quite sure how to do it.

ex

Customer name: ben sanders
Which package was purchased? A
How many hours were used? 700

Customer name: ben sanders
Package A
The total amount due is $ 1389.95
You would have saved $ 695.00 by selecting package B
You would have saved $ 1370.00 by selecting package C

>>if (package != 'A' || package != 'a')

The OR operator returns true if either side of the operator is true. Since package can not be both 'A' and 'a' at the same time the that snippet must always equate to true. On the other hand the AND operator only returns true if both sides of the operator are true and package can clearly be neither 'A' nor 'a' by being something like 'z' or 'Y', etc. Therefore, I suspect you want all the comparisons to be true in order for the code in the body of the statement to be run and therefore you want AND operators instead of OR operators.

>> if it exceed 744 need to put invalid

If it is to be invalid if the user used more than 744 hours but normal if they use more than 10, then someplace you will need to check. Maybe place the request to enter hours before the if statements and check if the input is valid:

bool inputNeeded = true;
while(inputNeeded)
{
  cout << " enter hours";
  cin >> hours;
  if(hours  > 10 && hours < 745)
     inputNeeded = false;)
}

Loops such as these are commonly used to insure proper input. The loop is said to be controlled by a flag or a sentinel. As long as the flag is true the loop continues.

>>im not quite sure how to do it.

How about calculating the cost under all three plans and comparing them, outputting the savings only if there are any.

please help me. heres my code right now

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	int hours; 
	double cost;
	string name;
	char package;
	cost = 0;

	cout <<"Enter customer name:";
	getline(cin, name);

	
	cout <<"select the following package:" << endl;
	cout <<"Package A $9.95 per month, 10 hours of access, additional hours are $2.00 per hour" << endl;
	cout <<"Package B $14.95 per month, 20 hours of access, additional hours are $1.00 per hour" << endl;
	cout <<"Package C $19.95 per month, unlimited hours" << endl;


	cout <<"Enter your choice:";
	cin >> package;

	if (package == 'A' || package == 'a')
	{
		cout <<"Enter hours used:";
		cin >> hours;

		if (hours > 10)
			cost = (9.95 + (hours - 10) * 2);
		else 
		cost = 9.95;

		
			cout <<"Customer name:" << name << endl;
			cout <<"Package:" << package << endl;
			cout <<"Your total amount due is:" << '$' << cost << endl;
	}


	if (package == 'B' || package == 'b')
	{
		cout <<"Enter hours used:";
		cin >> hours;
		
		if (hours > 20)
			cost = (14.95 + (hours - 20) * 2);
		
		else cost = 14.95;
				cout <<"Customer name:" << name << endl;
				cout <<"Package:" << package << endl;
				cout <<"Your total amount due is:" << '$' << cost << endl;
	}

	if (package == 'C' || package == 'c')
		
	{
		cout <<"Customer name:" << name << endl;
		cout <<"Package:" << package << endl;
		cout <<"Your total amount due is $19.95:" << endl;
		
	}

	if (package != 'A' && package != 'a' && package != 'B' && package != 'b' && package != 'C' && package != 'c')
	{
		cout <<"Invalid Package" << endl;

	}

	
	

	


	system("pause");
	return 0;
}

heres an example of the program run


Customer name: ben sanders
Which package was purchased? A
How many hours were used? 700

Customer name: ben sanders
Package A
The total amount due is $ 1389.95
You would have saved $ 695.00 by selecting package B
You would have saved $ 1370.00 by selecting package C

those 2 bold things idk how to do them how do i calculate them and where do those statments go. Also I still havent put if hours exceed 744 to cout invalid. thing is im already using hours in another calculation so i think it wont work if i put if (hours >= 7444) cout...
I just need these 2 things, help? =]

Also I still havent put if hours exceed 744 to cout invalid. thing is im already using hours in another calculation so i think it wont work if i put if (hours >= 7444)

It doesn't matter that you already used it once. Just add the line were you want.

those 2 bold things idk how to do them how do i calculate them and where do those statments go. cout...

Haven't got a clue what you mean with this^^ .

If you need to perform the two bold statements, then the best way would be something like this:

1. Prompt user for his name and package choice.
2. Prompt user for the number of hours.
3. Compute the cost for ALL three packages and store them in three different variables.
4. If user choose package A, display cost and possible savings if he choose package B and C. This would be simply the subtraction between the costs of different packages.
5. Repeat step 4 for package B.
6. Repeat step 4 for package C.

i though this would work but it didnt. sound easy but idk the coding

if (costb < cost)
		cout <<"You would have saved" << (costb - cost) <<"by selecting package B" << endl;
		costb = (14.95 + (hours - 20) * 1);

	 if (costc < cost)
		cout <<"You would haved saved" << (costc - cost) <<"by selecting package C" << endl;
		costc = (19.95 + hours);

1) Enter current plan
2) Enter current hours
3) calculate cost under plan A
4) calculate cost under plan B
5) calculate cost under plan C

Then you could do something like this:

switch(package)
{
   case 'A':
      if(costB < costA)
        display message
      if(costC < costA)
        display message
  cas 'B':
     if(cost A < costB)
        display message 
//....
}

OR even something like this;

double planCosts[3]; //costA = planCosts[0], etc

for(int i = 0; i < 3; ++i)
   for(int j = 0; j < 3; ++i)
      if(j != i)
        if(planCosts[j] < planCosts[i])
           display message

Be sure to use braces {} more often. It makes the code more readable, and you'll know you are grouping statements properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.