Errors

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;

double amtOwed = 0.0;
double changeAmt = 0.0;
double remain = 0.0;

cout << "Show amount paid: ";
cin >> amtPaid;
cout << "Show change: ";
cin >> changeAmt;

if
(amtOwed > amtPaid );
cout << "Amount paid is less than amout owed: " << endl;

else <---------ERRORS AT THIS LINE!!!!!
{
changeAmt = (amtPaid - amtOwed) * 100;
dollar = changeAmt / 100;
remain = changeAmt - dollar * 100;
quarter = remain / 25;
remain = remain - quarter * 25;
dime = remain / 10;
remain = remain - dime * 10;
nickel = remain / 5;
remain = remain - nickel * 5;
penny = remain / 1;
remain = remain - penny * 1;
cout << "Change Amount: " << changeAmt << endl;
cout << "Number of quarters: " << quarter << endl;
cout << "Number of dimes: " << dime << endl;
cout << "Number of nickels: " << nickel << endl;
cout << "Number of pennies: " << penny << endl;
} //end if


system("pause");
return 0;
} //end of main function
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.

Read this:

http://www.cplusplus.com/doc/tutorial/control/

and then change this

1
2
3
if 
(amtOwed > amtPaid );
cout << "Amount paid is less than amout owed: " << endl;

to this

1
2
3
4
if (amtOwed > amtPaid )
{
  cout << "Amount paid is less than amout owed: " << endl;
}


Last edited on
hiiii

U just Try it now!!!!!!!!!!!!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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;

double amtOwed = 0.0;
double changeAmt = 0.0;
double remain = 0.0;

cout << "Show amount paid: ";
cin >> amtPaid;
cout << "Show change: ";
cin >> changeAmt;

if(amtOwed > amtPaid )
cout << "Amount paid is less than amout owed: " << endl;

else 
{
changeAmt = (amtPaid - amtOwed) * 100;
dollar = changeAmt / 100;
remain = changeAmt - dollar * 100;
quarter = remain / 25;
remain = remain - quarter * 25;
dime = remain / 10;
remain = remain - dime * 10;
nickel = remain / 5;
remain = remain - nickel * 5;
penny = remain / 1;
remain = remain - penny * 1;
cout << "Change Amount: " << changeAmt << endl;
cout << "Number of quarters: " << quarter << endl;
cout << "Number of dimes: " << dime << endl;
cout << "Number of nickels: " << nickel << endl;
cout << "Number of pennies: " << penny << endl;
} //end if


system("pause");
return 0;


Can U Reply me if it works pls........
Last edited on
@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?

Last edited on
hi

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Change Amount.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare named constants and variables
	float dollar = 1.00;
	int dollars = 0;
	double quarter = 0.25;
	int quarters = 0;
	double dime = 0.10;
	int dimes = 0;
	double nickel = 0.05;
	int nickels = 0;
	double penny = 0.01;
	
	double amtPaid = 0.0;
	double amtOwed = 0.0;
	double changeAmt = 0.0;
	double change;

	cout << "Show amount owed: ";
	cin >> amtOwed;
	cout << "Show amount paid: ";
	cin >> amtPaid;

	if(amtOwed > amtPaid )
	{
		cout << "Amount paid is less than amount owed: " << endl;
		cout << "You still owe $" << amtOwed - amtPaid << endl;
	}
	else 
	{
		changeAmt = (amtPaid-amtOwed);
		change = changeAmt;

		if (changeAmt >= dollar)
		{
			dollars = changeAmt/dollar;
			changeAmt = changeAmt - dollars;
		}

		if (changeAmt >= quarter)
		{
			quarters = changeAmt/quarter;
			changeAmt = changeAmt - (quarter * quarters);
		}

		if (changeAmt >= dime)
		{
			dimes = changeAmt/dime;
			changeAmt = changeAmt - (dime * dimes);
		}

		if (changeAmt >= nickel)
		{
			nickels = changeAmt/nickel;
			changeAmt = changeAmt - (nickel * nickels);
		}

		if (changeAmt >= penny)
		{
			penny = changeAmt/penny;
		}
               else
			penny = 0;

		cout << "Your Change            : $" << change << endl << endl;
		cout << "Number of dollar bills : " << dollars << endl;
		cout << "Number of quarters     : " << quarters << endl;
		cout << "Number of dimes        : " << dimes << endl;
		cout << "Number of nickels      : " << nickels << endl;
		cout << "Number of pennies      : " << penny << endl;
	} //end if
        cout <<  << endl << endl << endl;
	cin.get();
	return 0;
}


Also, removed the system("pause"); which can mess things up. The program, when compiled, comes up with
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
, but still compiles and runs.
Last edited on
For bonus points, include two dollar bills in the change. :)
Or even half dollars.
Topic archived. No new replies allowed.