
- Forum
- Programming Talk
- C and C++
- the program is working but one problem
the program is working but one problem
This is a discussion on the program is working but one problem within the C and C++ forums, part of the Programming Talk category; #include <stdio.h> int main(); int getData() { int totalChange; printf("Please enter amount of change:"); scanf("%d", &totalChange); return(totalChange); } int errorResult(int ...
-
the program is working but one problem
#include <stdio.h>
int main();
int getData()
{
int totalChange;
printf("Please enter amount of change:");
scanf("%d", &totalChange);
return(totalChange);
}
int errorResult(int totalChange)
{
const int MaxChange = 95;
const int MinChange = 5;
int valid;
if(totalChange > MaxChange || totalChange < MinChange)
{
printf("Please enter the number between 5 and 95: \n");
valid = 0;
}
return(valid);
}
int makeChange(int *totalChange, int cent)
{
int number = 0;
int newChange;
newChange =(*totalChange % cent);
number =(*totalChange - newChange)/cent;
*totalChange = newChange;
return(number);
}
void PrintResult(int number, int cents)
{
printf("%dc = %d\n", cents, number);
return;
}
int main()
{
int totalChange, fiftyc, twentyc, tenc, fivec, valid;
const int FiftyCents = 50;
const int TwentyCents = 20;
const int TenCents = 10;
const int FiveCents = 5;
totalChange = getData();
valid = errorResult(totalChange);
if(valid)
{
fiftyc = makeChange(&totalChange, FiftyCents);
twentyc = makeChange(&totalChange, TwentyCents);
tenc = makeChange(&totalChange, TenCents);
fivec = makeChange(&totalChange, FiveCents);
PrintResult(fiftyc, FiftyCents);
PrintResult(twentyc, TwentyCents);
PrintResult(tenc, TenCents);
PrintResult(fivec, FiveCents);
}
return(0);
}
when i run the program like this:
please enter the amount of change: 0
please enter the number between 5 and 95:
and suddenly it quits the program,it doesn't want to continue
whats the problem? please help!
-
I think you can handle your problem.
-
change the if(valid) as if(Valid == 0) in your program.
-
Sponsored Ads

Reply With Quote





