My program is not working please help!!!!

Okay so I am trying to make a change counter program for my C++ class at EDCC. The program is supposed to takes in (a) a dollar amount for a purchase and (b) the amount tendered for the purchase. Then it should then give a listing of the change. The problem is that it does not read the input correctly and/or display it right. For example this is what it should do:

Welcome to Change Counter by Jeremy Red
Please enter the total amount of purchase: $52.173
$52.173
Please enter amount of money tendered: $60
$60.00
Your change is: $7.83
-------------------------------------------
Twenties : 0
Tens : 0
Fives : 1
Ones : 2
Quarters : 3
Dimes : 0
Nickels : 1
Pennies : 3
-------------------------------------------
Thank you for using Change Counter!

And this is what it actually does:

Welcome to change counter by Jeremy Red

Please enter the total amount of purchase : $52.173
1112584467

Please enter the amount tendered : $60
1114636288
Your change is : $0
-------------------------------------------
Twenties : 0
Tens : 0
Fives : 0
Ones : 0
Quarters : 0
Dimes : 0
Nickels : 0
Pennies : 0
-------------------------------------------
Thank you for using change counter!

So now you see my problem.
Here is my code:

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
  // Change_calculator.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <cmath>

int main(void)
{
	const int twenty = 2000;
	const short ten = 1000,five = 500,one = 100,quarter = 25,dime = 10,nickel = 5,penny = 1;
	long double purchase_total = 0;
    long double tendered_cash = 0;
	long long change = 0;
	int remainder = 0;
	int temp = 0;
	long long twenties,tens,fives,ones,quarters,dimes,nickels,pennys;
	printf("Welcome to change counter by Jeremy Red\n");
	printf("\nPlease enter the total amount of purchase : $");
	scanf("%f",&purchase_total);
	printf("%d \n",purchase_total);
	printf("\nPlease enter the amount tendered : $");
	scanf("%f",&tendered_cash);
	printf("%d \n",tendered_cash);
	change = int((tendered_cash - purchase_total)*100);
	printf("Your change is : $%d\n", change); 
	printf("-------------------------------------------\n");
	twenties = change / twenty;
	change %= twenty;
	tens = change / ten;
	change %= ten;
	fives = change / five;
	change %= five;
	ones = change / one;
	change %= one;
	quarters = change / quarter;
	change %= quarter;
	dimes = change / dime;
	change %= dime;
	nickels = change / nickel;
	change %= nickel;
	pennys = change / penny;
	printf("Twenties : %d \n",twenties);
	printf("Tens : %d \n",tens);
	printf("Fives : %d \n",fives);
	printf("Ones : %d \n",ones);
	printf("Quarters : %d \n",quarters);
	printf("Dimes : %d \n",dimes);
	printf("Nickels : %d \n",nickels);
	printf("Pennys : %d \n",pennys);
	printf("-------------------------------------------\n");
	printf("Thank you for using change counter!");
	scanf("%f",&tendered_cash);
	


	return 0;
}



*note: That last scanf is only there to keep the window from right away closing upon completion.


Please inform me of any mistakes I made(I think it has something to do with the scanf's) and how I can fix it. Also this is only my third week of C++ so if you could explain in it and write down how to fix it in as simplistic terms as possible that would be great.

Thank You,
Hi!

The problem is that you make a mess of format specifiers in scanf and printf.
%d should be used only for integer variables, while yours are "long double". %f is not good for "long double" too.

I believe it should be "%Lf" in both cases. However it is not necessary for you to use "long double" instead of simple double or float.

Try to read more about this:
http://www.cplusplus.com/reference/cstdio/scanf/
here or somewhere else. Shortly speaking, scanf should know how to fill memory address where variable resides and printf should know how to understand data which you pass. That is why format specifiers are necessary.
Last edited on
Topic archived. No new replies allowed.