My program runs but does not seem to do functions 3 and 4. What have I done wrong? It must be the syntax, I am guessing, but I keep going over it, and I can't find the problems....Any help would be very appreciated.
Thank you,
Erin
//This program calculates a customer's monthly bill
#include <iostream>
usingnamespace std;
char package; //Declaring variables
float hours, paymentA, paymentB;
float packA = 9.95;
float packB = 14.95;
float packC = 19.95;
float savingsOnPackB = paymentA - packB;
float savingsOnPackC = paymentA - packC;
void getPackage(); //Declaring prototypes
void getBill();
void savingsWithA();
void savingsWithB();
void getPackage() //Function Definition 1
{
cout << "Which package have you purchased?" << endl //Ask for package
<< "Please enter A, B, or C and press the Enter key." << endl;
cin >> package; //Read package
if(package == 'C') //If package is equal to C
{
cout << "Your payment is $" << packC << "." << endl; //Then payment is automatically $19.95
}
else //Or else, if package was NOT C,
{
cout << "How many hours were used during this billing cycle?" << endl //Ask for hours
<< "Please enter numerical amount and press the Enter key." << endl;
cin >> hours; //Read hours
}
}
void getBill() //Function Definition 2
{
if(package == 'A') //If package is equal to A,
{
if(hours <= 10) //And if hours were greater or equal to 10
{
cout << "Your payment is $" << packA << "." << endl; //Display payment of $9.95
}
else //Or else,
{
float paymentA = ((hours - 10) * 2) + packA; //Payment equals that plus $2/addirional hour
cout << "Your payment is $" << paymentA << endl; //Display payment
}
}
else //Or else,
{
if(package == 'B') //If package is B
{
if (hours <= 20) //And hours are less than or equal to 20
{
cout << "Your payment is $" << packB << "." << endl; //Display payment of $14.95
}
else //Or else,
{
float paymentB = ((hours - 20) * 1) + packB; //Payment is that plus additional $1/hhour
cout << "Your payment is $" << paymentB << endl; //Display payment
}
}
}
}
void savingsWithA() //Function Definition 3
{
if (package == 'A') //If package is A
{
if(paymentA > packC) //If the payment was greater than the cost of package C
{
cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
}
}
else //Or else
{
if(paymentA > packB) //If the payment was greater than the cost of package B
{
cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
}
}
}
void savingsWithB() //Function Definition 4
{
if(package == 'B') //If package was B
{
if(paymentB > packC) //If payment was greater than the cost of package C
{
cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
}
}
}
int main() //Begin main program
{
getPackage(); //Call functions
getBill();
savingsWithA();
savingsWithB();
return 0; //Quit
}
Oh, I'm sorry. Sure. My program does what the first two functions were written to do: It accepts the Package when input, and it displays how much the bill is depending on which package was purchased. What it does not do is then calculate and display the savings amount if Packages B or C had been chosen.
The program is supposed to ask which package you have.
Then it should tell you what your bill is.
Then, it should tell you the amount you would have saved with either package B or C.
If no savings would have occurred, then no message regarding savings should be displayed.
Does that help? I feel like I'm really close (I hope!) but I can't seem to figure out the problem....
if(paymentA > packB) //If the payment was greater than the cost of package B
{
float savingsOnPackB = paymentA - packB;
cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
}
(sic)
In function savingsWithA(), on line 71, if paymentA is greater than packB, the following block is executed, otherwise, the else is executed. Since it's not printing, control must've been passed to else.
ErinCorona wrote:
1 2 3
else
{
if(paymentA > packC)
(sic)
So, the proposition in else is tested. If the evaluation yields false, the following block isn't executed. Again, since it never prints, the proposition must've been false. Since there's no associated else, you're telling the computer that you're refusing to handle the scenario when the proposition yields false.
Since savingsWithB() is more or less the same as savingsWithA(), the latter can be applied to either function. Your main problem is your program's logic.
Note: I'll update this post with relevant information regarding your code.
It's okay, no worries, and thanks so much for even responding. This one has taken me the better part of a week, I'm embarrassed to admit. But....I think I got it! I'm so excited. Here's what I ended up with. Once I moved the variable declarations within each function to the very top of each function, it worked. :) I still have no idea how to put everything into an output file to send to my teacher, but I'm really excited that I got my program to work right. :)
I couldn't just leave it alone...no. I had to try and make it better. I'm trying to test it when the package gets input at the beginning. I'm trying to make sure it is either A, B, or C, or else I would like to display a message asking for A, B, or C. I thought that was the correct use of a while loop, but I've done it wrong, because it just loops forever. I will keep at it, but if there's anything you see right away, please give a girl a hint.
And thanks for all of your help. I have emailed my teacher questions (last week) but still gotten no reply, so this site is a godsend. :) I'm learning a lot.
//This program calculates a customer's monthly bill
#include <iostream>
usingnamespace std;
char package; //Declaring variables
float hours;
float packA = 9.95;
float packB = 14.95;
float packC = 19.95;
float paymentA = ((hours - 10) * 2) + packA;
float paymentB = ((hours - 20) * 1) + packB;
void getPackage(); //Declaring prototypes
void isPackageC();
void getBill();
void savingsWithA();
void savingsWithB();
void getPackage() //Function Definition 1
{
cout << "Which package have you purchased?" << endl //Ask for package
<< "Please enter A, B, or C and press the Enter key." << endl;
cin >> package; //Read package
}
void isPackageC() //Function Definition 2
{
if(package == 'C') //If package is equal to C
{
cout << "Your payment is $" << packC << "." << endl; //Then payment is automatically $19.95
}
else //Or else, if package was NOT C,
{
cout << "How many hours were used during this billing cycle?" << endl //Ask for hours
<< "Please enter numerical amount and press the Enter key." << endl;
cin >> hours; //Read hours
}
}
void getBill() //Function Definition 3
{
if(package == 'A') //If package is equal to A,
{
if(hours <= 10) //And if hours were greater or equal to 10
{
cout << "Your payment is $" << packA << "." << endl; //Display payment of $9.95
}
else //Or else,
{
float paymentA = ((hours - 10) * 2) + packA; //Payment equals that plus $2/additional hour
cout << "Your payment is $" << paymentA << endl; //Display payment
}
}
else //Or else,
{
if(package == 'B') //If package is B
{
if (hours <= 20) //And hours are less than or equal to 20
{
cout << "Your payment is $" << packB << "." << endl; //Display payment of $14.95
}
else //Or else,
{
float paymentB = ((hours - 20) * 1) + packB; //Payment is that plus additional $1/hour
cout << "Your payment is $" << paymentB << endl; //Display payment
}
}
}
}
void savingsWithA() //Function Definition 4
{
float paymentA = ((hours - 10) * 2) + packA;
float savingsOnPackB = paymentA - packA;
float savingsOnPackC = paymentA - packC;
if (package == 'A') //If package is A
{
if(paymentA > packC) //If the payment was greater than the cost of package C
{
cout << "You would save $" << savingsOnPackC << " if you upgraded to Package C." << endl; //Then display savings
}
else
{
if(paymentA > packA) //If the payment was greater than the cost of package A
{
cout << "You would save $" << savingsOnPackB << " if you upgraded to Package B." << endl; //Display savings
}
}
}
}
void savingsWithB() //Function Definition 5
{
float paymentB = ((hours - 20) * 1) + packB;
float savingsOnPackC = paymentB - packC;
if(package == 'B') //If package was B
{
if(paymentB > packC) //If payment was greater than the cost of package C
{
cout << "You would save $ " << savingsOnPackC << " if you upgraded to Package C." << endl; //Display savings
}
}
}
int main() //Begin main program
{
getPackage(); //Call functions
while(package != 'A'|| 'B'|| 'C') //While package entered is NOT A, B, or C
{
cout << "Please enter either A, B, or C." << endl; //Prompt for A, B, or C
}
isPackageC(); //Otherwise, go on with the program
getBill();
savingsWithA();
savingsWithB();
return 0; //Quit
}
I've corrected a few small problems in your code. I moved the while loop into the getPackage() function and set package input, to upper case, so both upper and lower cases have to be checked. Also, in savingsWithA() function, float savingsOnPackB = paymentA - packA;, was changed to float savingsOnPackB = paymentA - packB; Hope this helps you..
Thank you so much. In the car on the way to work, I realized my while loop had to move, and I was going to tackle the uppercase problem next. You read my mind. Thanks very much. A few questions if you don't mind, so I can better understand:
1) Why change the datatypes to double from float? Wouldn't floats have worked, or would they have given me a problem somewhere?
2) When typing functions, I see that you changed the getPackage from a 'void' to a 'char' so how do I know what those need to be? Is there a question I need to ask for every function to know which type it falls under?
3) In the getPackage function, your return statement says "return package." What exactly does that mean/do?
4) In the main program, the first function says "package = getPackage();" Why couldn't if have just read getPackage();? What does that first part do?
Okay, I'm sorry for all the questions, and if you don't get around to answering them, I'll figure them out.
Thanks a lot for your help, again.
:)
Okay, I'll see how well I can explain the answers to your questions.
1) I was getting a warning from my compiler. warning C4305: 'initializing' : truncation from 'double' to 'float' So, I changed them. No more warnings.
2) A void function means it doesn't get sent, or return, anything. A char is any one character. So, I was just requesting, from the function, a letter. As you see, though, not every function needs to return a variable, or receive one. So they can be void.
3) The char named package is sent to the getPackage function. The return package then gave the calling variable a value.
4) Yes, it could be written that way, and it does work. But I find it easier to understand what is happening in the program. By having "package = getPackage();", I know what variable is being used, and which will be changed.
I'm probably not to good at explaining my reasonings for doing things, so if you want, or need, better understanding of what I did, please ask. I promise to answer to the best of my ability
Your reasons are good enough for me. Thanks. And I'm actually online now studying up on how to put all this in an output file. I understand how to create one, but is there a way to keep appending it, for each time I run the program? Or could you point me to a tutorial webpage, perhaps , if you don't want to just tell me the answer?
This is the page I'm on now, but so far, nothing about appending an output file... http://www.cplusplus.com/doc/tutorial/files/
Thanks so much! You really have been so helpful. (STILL no answer from my actual teacher. :( )