change counter c program not displaying correct output - c

I am working on a change counter program and I am stuck, I have searched here and 2 other coding forums, google and youtube, but haven't found an answer yet...my program sucks and i am brand new to c/c++...my txt book is not here yet so I am trying to read every every thing i can get my hands on till it gets here
This is what the output should be in the console window:
Welcome to Change Counter by Jo Mama!!
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!
here is what i have so far...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
// define variables here
long double numberOfTwenties = 0;
long double numberOfTens = 0;
long double numberOfFives = 0;
long double numberOfOnes = 0;
long double numberOfQuarters = 0;
long double numberOfDimes = 0;
long double numberOfNickels = 0;
long double numberOfPennies = 0;
double purchasePrice = 0;
double amountTendered = 0;
double amountOfChange = 0;
double amountOfChangeCents = 0;
// start program here
printf("Welcome to the change counter by Josh Manion!!\n\n");
printf("Please enter the total amount of purchase: $");
scanf("%d", &purchasePrice);
printf("%d\n", purchasePrice);
printf("Please enter amount of money tendered: $");
scanf("%d", &amountTendered);
printf("%d\n", amountTendered);
//do change calculations here
amountOfChange = (amountTendered - purchasePrice);
printf("Your change is: $%d\n", amountOfChange);
numberOfTwenties = amountOfChange / 20;
//amountOfChange = numberOfTwenties %= amountOfChange;
numberOfTens = (amountOfChange / 10);
numberOfFives = (amountOfChange / 5);
numberOfOnes = (amountOfChange / 1);
numberOfQuarters = (amountOfChange * 0.25);
// print change calculations here
printf("---------------------------------------------\n");
//display denominations of change here
printf("Twenties: %d\n", numberOfTwenties);
printf("Tens: %d\n", numberOfTens);
printf("Fives: %d\n", numberOfFives);
printf("Ones: %d\n", numberOfOnes);
printf("Quarters: %d\n", numberOfQuarters);
printf("Dimes: %d\n", numberOfDimes);
printf("Nickels: %d\n", numberOfNickels);
printf("Pennies: %d\n", numberOfPennies);
printf("---------------------------------------------\n");
printf("Thank you for using the Change Counter!");
getchar();
return EXIT_SUCCESS;
}
The problem is that my program doesn't work, it shows the change after the prompt but not the breakdown of denominations, I am sposed to use the "%" to bring down the change amount, but i havent found any examples. I don't know what else to say... besides I'm new and this post will help a lot of people...

Your program has undefined behaviour : you must use %Lf as the printf modifier to print a long double (same issue with your usage of scanf)

Related

Compound interest calculator giving incorrect answer

I have written a program to calculate compound interest.
Here is the code:
#include <stdio.h>
#include <math.h>
int main(void) {
float value, rate, years,r;
int column = 0, tmp;
printf("Enter money values: ");
scanf("%f",&value);
printf("Enter a interest rate: ");
scanf("%f",&rate);
printf("Enter number of years: ");
scanf("%f",&years);
printf("\nYears ");
tmp = rate + 4;
r = rate;
for (int a = rate; a <= tmp; a++) {
printf(" %d ", a);
column++;
}
for (int b = 1; b <= column; b++) {
printf("\n %d",b);
for (int i = 1; i<= column; i++) {
printf(" %.2f ", (float) pow ( (value)*(1.0+((r/100.0)/(1.0))) , (1.0*b))
r++;
}
r = rate;
printf("\n");
}
// I = P*R*T
// P= AMOUNT (value)
// R=RATE (r)
// T=YEARS (b)
return 0;
}
It asks the user for a value (money), interest rate, number of years and displays the interest rate like so:
Enter money values: 100
Enter a interest rate: 6
Enter number of years: 5
Years 6 7 8 9 10
1 106.00 107.00 108.00 109.00 110.00
2 11236.00 11449.00 11664.00 11881.00 12100.00
3 1191016.00 1225043.00 1259712.00 1295029.00 1331000.00
4 126247696.00 131079600.00 136048896.00 141158160.00 146410000.00
5 13382255616.00 14025517056.00 14693280768.00 15386240000.00 16105100288.00
But my problem is the floating point calculation.
As you may be able to tell the numbers in the output above and very long and have many trailing digits
which i am very confused about.
For example in the 2nd row the first output is 11236.00,
this is wrong since it should be outputting 112.36 but for some reason the decimal has moved
forward two spaces. Why is this? and how could i fix this problem and print the correct solution
with the decimal in the correct place.
You have the value inside the pow. So when you square for two years, you are squaring the amount. Move the (value)* to output the pow call.

C Unhandled exception 0xC0000094: Integer division by zero

I have been given this error for two days straight, and I have worked on it for about three hours, altogether. When I run it, it will tell me how many $10 bills I need but just stops at the $5 line and gives me this exception every time.
#define _CRT_SECURE_NO_WARNINGS 'code'
#include <stdio.h>]
#include <stdlib.h>
#include "math.h"
int main()
{
int ten = 10;
int five = 5;
int one = 1;
float quarter = .25;
float dime = .10;
float nickel = .05;
float penny = .01;
double money=0;
printf("Please enter a monetary amount:");
scanf_s("%lf", &money);//scanning the entry in, and & is allowing it to be entered
//money is the input number
printf("You entered: %lf \n", money);//creates too many zeroes but for now move on
ten = money / ten;//dividing the change = 10, and then
money = (int) money % ten;//this determines how many tens there are in the mix
//casted it because it only needs to be an integer not a double
printf("$10 : %d \n", ten);//printing an integer of how many tens are needed to make up money
five = money / five;//dividing the change = 10, and then
money = (int) money % five;//this determines how many tens there are in the mix
//casted it because it only needs to be an integer not a double
printf("$5 : %d \n", five);
//first problemm I had was putting in const, and that gave me a bajillion errors
return(EXIT_SUCCESS);
}
The error that you have mentioned will be shown if you divide a number by zero.just like hanie mentioned in her answer but in your case i don't see a line were you are dividing a number by zero but you have some lines were you overwrite the ten and five variables using modulo operator i guess it might be throwing the error.
I am posting a modified version of your code. Hope it helps you.
#define _CRT_SECURE_NO_WARNINGS 'code'
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
int main()
{
int tens = 0;
int fives = 0;
double money=0;
printf("Please enter a monetary amount:");
scanf("%lf", &money);
printf("You entered: %lf \n", money);
tens = money / 10;
printf("$10 : %d \n", tens);
money = money - tens * 10;
fives = money / 5;
printf("$5 : %d \n", fives);
money = money - fives * 5;
return(EXIT_SUCCESS);
}
Sample Input and Output
Please enter a monetary amount:125
You entered: 125.000000
$10 : 12
$5 : 1
Please enter a monetary amount:123
$10 : 12
$5 : 0
Note that most implementations of floating point math will follow a standard (e.g. IEEE 754), in which case operations like divide-by-zero will have consistent results and the C standard says the operation is undefined.
before doing division you need to do something like this:
if(denominator != 0)
a = a / denominator;
or
if(denominator != 0)
a = a % denominator;

Infinite loop in c due to rounding?

The following code is looping infinitely. I believe it may be a rounding issue though not entirely sure.
I am fairly new to C so not sure why I'm getting the infinite looping that I'm getting. The code seems to make sense though keeps looping.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
int main(void){
float quarter = .25;
int Qtr = 0;
float dime = .10;
int Dm = 0;
float nickel = .5;
int Nck = 0;
float penney = .01;
int Pn = 0;
float change;
float userInput;
float newspaperCost;
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
}
while(newspaperCost <= 0);
while(change > 0){
printf("%f\n", change);
while(change - quarter > 0){
change = change - quarter;
Qtr++;
}
while(change - dime > 0){
change = change - dime;
Dm++;
}
while(change - nickel > 0){
change = change - nickel;
Nck++;
}
while(change - penney > 0){
change = change - penney;
Pn++;
}
}
printf("Your change consists of %d quarters, %d dimes, %d nickels, and %d pennies\n", Qtr, Dm, Nck, Pn);
} //end main
You have a logic error in the last while loop.
Instead of
while(change - penney > 0){
change = change - penney;
Pn++;
}
Use
while(change > 0){
change = change - penney;
Pn++;
}
Don't use floating point to represent currency.
Prefer double over float unless you have a good reason.
penny*
You're not using any equality operators with floating point operands, so you probably have a logical error.
Your code:
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
} while(newspaperCost <= 0);
Won't exit this loop while newspaperCost <= 0. I'm assuming you aren't inputting a negative number at
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
That's why you have an infinite loop.

my code has exited with code 0 (0x0)

This is the original problem : ""A class of ten students took a quiz. The grades for this
quiz are available to you. Determine the class average on the quiz""
int main(){
float average, total, currentnote, i;
average,total = 0;
for (i = 0; i < 10; i++){
printf("please enter the quiz result:");
scanf_s("%d", &currentnote);
total = total + currentnote;
if (i = 9){
average = total / 10;
printf("result equals= %f", average);
getchar();
}
}
}
After I enter one number it exits with error
The program '[3272] Project1.exe' has exited with code 0 (0x0).
Can someone show me my error?
Besides having to test for equality as if (i == 9),
you can't set two variables equal to a common value this way: average, total = 0
Do average = total = 0 instead.
The currentnote variable is a float - but your format flag for scanf_s is telling it to expect an int.
For clarity of intention in the code, why is this inside the for loop? Why not put it after the for loop?
if (i == 9){
average = total / 10;
printf("result equals= %f", average);
getchar();
}
if (i = 9)
Should be
if (i == 9)
See if that's the problem.
You wrote:
scanf_s("%d"....
But %d is for ints, and you have a float.
Use %f.

C Program Exercise. Simple transaction report

Question : Program that asks the user to enter an item price value and then show how to pay that amount using the smallest number of $ 50,$20, $10,$5, and $1 bills consist.
Example Output:
Enter Price: 187
Enter Amount to pay: 500
Change is : 313
(6)$50 (1)$10 (3)$1
(0)$20 (0)$5
Here's my code: hope you help me , I am having a hard to in knowing the right formula for it..
#include <stdio.h>
#include <conio.h>
#define p printf
#define s scanf
#define g gotoxy
main()
{
clrscr();
int c1,c2,c3,c4,c5;
int price,amount;
float change;
p("Enter Price: ");s("%d",&price);
p("Enter amount: ");s("%d",&amount);
change=amount-price;
p("Change is : %f ",change);
c1=(change/50);
c2=(0);
c3=(change/change);
c4=(0);
c5=(change/change)+2;
g(5,5);p("(%d) Php 50",c1);
g(5,6);p("(%d) Php 20",c2);
g(18,5);p("(%d)Php 10 \t",c3);p("(%d)Php 1",c5);
g(18,6);p("(%d) Php 5 ",c4);
getch();
return 0;
}
You're on the right track:
change should be a int too (that means you should change %f to %d). You would then correctly determine the number of 50's (note that integer division in C truncates). You should look at % (modulus operator) to get the remaining amount of changes after the 50's are dealt with:
Using your example:
change = 313
fifties = 313/50 (6)
change %= 50 (13)
That means set change to the remainder after dividing itself by 50 (change = change % 50)
twenties = change / 20 (0)
change %= 20 (13)
tens = change / 10 (1)
change %= 10 (3)
This should give you the basic idea of the code you need. You just continue this pattern in order of decreasing denomination.
As noted, use better variable names, don't use those defines, and generally stick to one statement per line (add a newline after semi-colons). This will make your code more readable. You're also using more parentheses than needed, but that's not a big deal.
I would suggest define an array which holds the bill denominations, and an initially empty array of bill counts:
int denoms[5] = {50, 20, 10, 5, 1};
int bills[5] = {0, 0, 0, 0, 0};
for(int i =0; i < 5; ++i)
{
bills[i] = /* do something interesting with denoms[i] here */
change = /* more work for you here */
}
/* output answer */
for(int i =0; i < 5; ++i)
{
if (bills[i] > 0)
p("{%d)$%d", bills[i], denoms[i]);
}
p("\n");
for(int i =0; i < 5; ++i)
{
if (bills[i] == 0)
p("{%d)$%d", bills[i], denoms[i]);
}
p("\n");
void changeloop(int* change, int* counter, int amount) {
while (*change > amount) {
(*counter)++;
(*change) -= amount;
}
}
int main() {
clrscr();
int price; printf("Enter Price: "); scanf("%d", &input);
int amount; printf("Enter Amount: "); scanf("%d", &amount);
int change = amount - price;
int fifties, twenties, tens, fives, ones;
fifties = twenties = tens = fives = ones = 0;
changeloop(&change, &fifties, 50);
changeloop(&change, &twenties, 20);
changeloop(&change, &tens, 10);
changeloop(&change, &fives, 5);
changeloop(&change, &ones, 1);
printf("Fifties: %d\n", fifties);
printf("Twenties: %d\n", twenties);
printf("Tens: %d\n", tens);
printf("Fives: %d\n", fives);
printf("Ones: %d\n", ones);
getch();
return;
}
There's work to do, like input validation and error handling. But the basics are here. The code could be refactored to be much more extensible... but meh.

Resources