I have a program I built tht is supposed to display change after a purchase. So far this is what I have and I keep gettin two erros at line 28.
The error states illegal else without matching if and Intellisense expected statement. HELP PLEASE!!! What did I do wrong???
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//declare named constants and variales
int dollar = 0;
int quarter = 25;
int dime = 10;
int nickel = 5;
double penny = 1;
double amtPaid = 0.0;
if (amtOwed > amtPaid );
See that semi-colon? That's the end of your if block. Your if block has no code in it. Nothing happens. The semi-colon marks the end of things to do.
@srija: I had to tweek it just a bit, but yes it worked!
Now my problem is the program did not run as expected. I am supposed to enter an amount and an amount the customer paid, at that time the program is supposed to show me how many dollars, quarters, dimes, nickels, and pennies the customer is to receive. Well it didn't work properly. Do you notice anything in my prgram that could be wrong?
@moschops: the funny thing about that part being wrong; I had it set like that when I started and the programming tutor at my college said, "Nope bring it to it's own line under the "if" statement. Now, I have it building correctly, but it won't calculate the change. Am I missing some code somewhere?
"Nope bring it to it's own line under the "if" statement.
This if(amtOwed > amtPaid )
is identical to this
1 2
if
(amtOwed > amtPaid )
in terms of what the compiler sees. It's just convention to do it the first way. If your programming tutor prefers the second way, that's fab; just be aware that it's just a matter of style and doesn't make any difference. The semi-colon, however, makes a big difference.
The problem wasn't having it on the next line. The problem was that you had a semi-colon on the end of the if condition, which marked out the end of all the code to execute if the condition was met.
It is calculating the change. Note that you haven't asked it to output the number of dollars in the change, so if the change is an exact number of dollars, everything else will be zero.
I see that amtOwed is hard-coded to 0.0 and you never change it. Is that meant to be the case, or are you doing that later?
1.U have declared and initialized double amtOwed = 0.0; and u r entering the amount paid through the keyboard sure it will be greater than 0 . so the If condition wont be true forever and it automatically runs the code block in the else statement.
2. May i Know what is the variable changeamt(for what it is?)
It shows the amount of change the consumer is to receive. When I run the program, it asks for me to enter the amount the customer is to pay, then, the amount they paid. From there the program is to calculate the number of quarters, dimes, nickels, and pennies the customer is to receive. I think dollars is supposed to be in there to, but I have to add that code.
@moschops: amtOwed 0.0; is set like that, because when she looked at the code to check what I had, she said it was supposed to be 0.0. Would it be better as 0;
@Amy269
You are having a lot of problems due to subtracting dollars, quarters, etc. from remains, especially if the remains was less than the subtracting. Here is the program with checks.