Reputation: 149
I'm just beginning studying programming and am trying to write a program that will display how many of each denomination of currency is required for any given amount of change. I'm studying in Japan, so the currency is yen, but I think the basic code is universal. I've seen other similar programs online, but mine has a few extra features which may be the cause of my problem, but I'm not sure.
First the user inputs whether or not there are two-thousand yen bills in the register or not. (as these bills are not common). Then you enter the total amount due. Then enter how much was paid. it then calculates the change and how much of each denomination and then displays it.
However, after you enter the amount paid, the cursor goes to the next line and just sits there indefinitely. I don't know what's causing this. My only guess is that it's getting stuck in a loop somewhere.
Does anyone see a problem? (*I switched the text to be printed to English)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
//入力
int aru;
printf("Are there 2-thousand yen bills in the register?\n 1.) Yes\n 2.) No\n "); //レジに2千円札が入ってますか?\n 1.) 入ってます\n 2.)入ってません
scanf("%d", &aru);
int total, paid;
printf("Enter Total Price ");//お会計を記入して下さい。
scanf("%d", &total);
printf("Enter Amount Paid ");//お客さんのお支払った合計を記入してください。
scanf("%d", &paid);
//計算
if (paid < total)
{
printf("Insufficiant amount paid\n");//お金を十分にもらいませんでした
}
if (paid > total)
{
int change = paid - total;
int ichi = 0, go = 0, ju = 0, goju = 0;
int hyaku = 0, gohyaku = 0, sen = 0, nisen = 0, gosen = 0;
while (change > 5000)
{
change - 5000;
gosen++;
}
while (change > 2000)
{
if (aru == 1)
{
change - 2000;
nisen++;
}
else
{
nisen = 0; //skips calculating 2000 yen bills if answer was 'no'
}
}
while (change > 1000)
{
change - 1000;
sen++;
}
while (change > 500)
{
change - 500;
gohyaku++;
}
while (change > 100)
{
change - 100;
hyaku++;
}
while (change > 50)
{
change - 50;
goju++;
}
while (change > 10)
{
change - 10;
ju++;
}
while (change > 1)
{
change - 1;
ichi++;
}
//出力
printf(" %d \n", gosen);
printf(" %d \n", nisen);
printf(" %d \n", sen);
printf(" %d \n", gohyaku);
printf(" %d \n", hyaku);
printf(" %d \n", goju);
printf(" %d \n", ju);
printf(" %d \n", go);
printf(" %d \n", ichi);
}
return 0;
}
Upvotes: 0
Views: 333
Reputation: 7542
while (change > 5000) //This is an infinite loop
{
change - 5000; //no change is made to change
gosen++;
}
You might want change -= 5000;
instead of change - 5000;
This is at several places in your code.
change-=5000
is equivalent to
change = change-5000;
Upvotes: 2