I can't see the Error, Newbie needs help
I run the compiler and the code is ok for that, but it won't do the calculations or the looping i think. I can't wee where i have gone wrong, and i have to hand this in on Tuesday. Can anyon e see where i have screwed up. Any help is greatly appreciated.
Code:
/* Assignment 4 - Bonus
Variables: int MaxCoins, NumCoin,
float CurrentAmt,AmtRemaining
*/
#include <stdio.h>
#include<stdlib.h>
#define TOONIE 2.00
#define LOONIE 1.00
#define JOHNIE 1.23
#define QUARTER 0.25
#define DIME 0.10
#define NICKEL 0.05
#define PENNY 0.01
int main()
{
float Currentamt,AmtRemaining;
int i,maxCoins,NumCoins,maxJohnie;
int NumToonie,NumLoonie,NumJohnie,NumQuarter,NumDime,NumNickel,NumPenny;
Currentamt = 0;
maxCoins = 10000;
maxJohnie/Currentamt;
printf("\nEnter an amount of money that is less than $1000.00 (Enter a 0 to quit): ");
scanf("%f",&Currentamt);
while(Currentamt != 0)
{
AmtRemaining = Currentamt;
for(i = 0; i <= maxJohnie; i++)
{
AmtRemaining = AmtRemaining - 1.23;
NumCoins = NumCoins + 1;
NumJohnie = NumJohnie + 1;
}
while (AmtRemaining >= TOONIE)
{
AmtRemaining = AmtRemaining - 2;
NumCoins = NumCoins + 1;
NumToonie = NumToonie + 1;
}
while (AmtRemaining >= LOONIE)
{
AmtRemaining = AmtRemaining - 1;
NumCoins = NumCoins + 1;
NumLoonie = NumLoonie + 1;
}
while (AmtRemaining >= QUARTER)
{
AmtRemaining = AmtRemaining - .25;
NumCoins = NumCoins + 1;
NumQuarter = NumQuarter + 1;
}
while (AmtRemaining >= DIME)
{
AmtRemaining = AmtRemaining - .10;
NumCoins = NumCoins + 1;
NumDime = NumDime + 1;
}
while (AmtRemaining >= NICKEL)
{
AmtRemaining = AmtRemaining - .05;
NumCoins = NumCoins + 1;
NumNickel = NumNickel + 1;
}
while (AmtRemaining >= PENNY)
{
AmtRemaining = AmtRemaining - .01;
NumCoins = NumCoins + 1;
NumPenny = NumPenny + 1;
}
printf("\nThe least amount of coins required to make up $%f is %d",Currentamt,NumCoins);
printf("\nThe coins need are: ");
if(NumToonie != 0)
printf("\n %d \t Toonies \t = \t $%f",NumToonie,NumToonie*TOONIE);
if(NumLoonie != 0)
printf("\n %d \t Loonies \t = \t $%f",NumLoonie,NumLoonie*LOONIE);
if(NumJohnie != 0)
printf("\n %d \t Johnies \t = \t $%f",NumJohnie,NumJohnie*JOHNIE);
if(NumQuarter != 0)
printf("\n %d \t Quarters \t = \t $%f",NumQuarter,NumQuarter*QUARTER);
if(NumDime!= 0)
printf("\n %d \t Dimes \t = \t $%f",NumDime,NumDime*DIME);
if(NumNickel != 0)
printf("\n %d \t Nickel \t = \t $%f",NumNickel,NumNickel*NICKEL);
if(NumPenny != 0)
printf("\n %d \t Pennys \t = \t $%f",NumPenny,NumPenny*PENNY);
printf("\n %d coins \t \t = \t $%f", NumCoins,Currentamt);
printf("\nEnter an amount of money that is less than $1000.00 (Enter a 0 to quit): ");
scanf("%f",&Currentamt);
if(NumCoins <= maxCoins)
{ maxCoins = NumCoins;
NumJohnie = i;
}
}
getchar();
return 0;
}
This is my first post so if i screwed it up,, bare with me and tell me what i did wrong so i can fix it for next time.. meanwhile
WHY won't this work..??
Thanks
Thank you very much for your help, it worked
Quote:
Originally Posted by ssharish
Originally Posted by
pass_prime
Code:
/* Assignment 4 - Bonus
Variables: int MaxCoins, NumCoin,
float CurrentAmt,AmtRemaining
*/
#include <stdio.h>
#include<stdlib.h>
#define TOONIE 2.00
#define LOONIE 1.00
#define JOHNIE 1.23
#define QUARTER 0.25
#define DIME 0.10
#define NICKEL 0.05
#define PENNY 0.01
int main()
{
float Currentamt,AmtRemaining;
int i,maxCoins,NumCoins,maxJohnie;
int NumToonie,NumLoonie,NumJohnie,NumQuarter,NumDime,N umNickel,NumPenny;
Currentamt = 0;
maxCoins = 10000;
maxJohnie/Currentamt;
printf("\nEnter an amount of money that is less than $1000.00 (Enter a 0 to quit): ");
scanf("%f",&Currentamt);
while(Currentamt != 0)
{
AmtRemaining = Currentamt;
for(i = 0; i <= maxJohnie; i++)
{
AmtRemaining = AmtRemaining - 1.23;
NumCoins = NumCoins + 1;
NumJohnie = NumJohnie + 1;
}
while (AmtRemaining >= TOONIE)
{
AmtRemaining = AmtRemaining - 2;
NumCoins = NumCoins + 1;
NumToonie = NumToonie + 1;
}
while (AmtRemaining >= LOONIE)
{
AmtRemaining = AmtRemaining - 1;
NumCoins = NumCoins + 1;
NumLoonie = NumLoonie + 1;
}
while (AmtRemaining >= QUARTER)
{
AmtRemaining = AmtRemaining - .25;
NumCoins = NumCoins + 1;
NumQuarter = NumQuarter + 1;
}
while (AmtRemaining >= DIME)
{
AmtRemaining = AmtRemaining - .10;
NumCoins = NumCoins + 1;
NumDime = NumDime + 1;
}
while (AmtRemaining >= NICKEL)
{
AmtRemaining = AmtRemaining - .05;
NumCoins = NumCoins + 1;
NumNickel = NumNickel + 1;
}
while (AmtRemaining >= PENNY)
{
AmtRemaining = AmtRemaining - .01;
NumCoins = NumCoins + 1;
NumPenny = NumPenny + 1;
}
printf("\nThe least amount of coins required to make up $%f is %d",Currentamt,NumCoins);
printf("\nThe coins need are: ");
if(NumToonie != 0)
printf("\n %d \t Toonies \t = \t $%f",NumToonie,NumToonie*TOONIE);
if(NumLoonie != 0)
printf("\n %d \t Loonies \t = \t $%f",NumLoonie,NumLoonie*LOONIE);
if(NumJohnie != 0)
printf("\n %d \t Johnies \t = \t $%f",NumJohnie,NumJohnie*JOHNIE);
if(NumQuarter != 0)
printf("\n %d \t Quarters \t = \t $%f",NumQuarter,NumQuarter*QUARTER);
if(NumDime!= 0)
printf("\n %d \t Dimes \t = \t $%f",NumDime,NumDime*DIME);
if(NumNickel != 0)
printf("\n %d \t Nickel \t = \t $%f",NumNickel,NumNickel*NICKEL);
if(NumPenny != 0)
printf("\n %d \t Pennys \t = \t $%f",NumPenny,NumPenny*PENNY);
printf("\n %d coins \t \t = \t $%f", NumCoins,Currentamt);
printf("\nEnter an amount of money that is less than $1000.00 (Enter a 0 to quit): ");
scanf("%f",&Currentamt);
if(NumCoins <= maxCoins)
{ maxCoins = NumCoins;
NumJohnie = i;
}
}
getchar();
return 0;
}
well, the code which is nin bold should be like this
Code:
maxJohnie/=Currentamt; // which is equal to maxJohnie=maxJohine/Currentamt
s.s.harish
that worked, the program now does the looping and adding, thanks, even though i have the math incorrect, i now can work on that because i can see what is happening,, many thanks for your help.
You have made one happy person out of me,, as usual it is the small thngs that make or break a code.