I can't see the Error, Newbie needs help

This is a discussion on I can't see the Error, Newbie needs help within the C Programming forums, part of the General Programming Boards category; I run the compiler and the code is ok for that, but it won't do the calculations or the looping ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Location
    Kenora Ontario
    Posts
    6

    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
    Last edited by pass_prime; 06-19-2005 at 07:58 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    well, the code dosen't even compile, u have made lots of syntax error, check out these

    1. make sure all the key words starts with low case letters
    2. have all the variables declared
    3. check your logic is correct

    - s.s.harish

  3. #3
    Registered User
    Join Date
    Jun 2005
    Location
    Kenora Ontario
    Posts
    6

    thanks for the help

    Quote Originally Posted by ssharish
    well, the code dosen't even compile, u have made lots of syntax error, check out these

    1. make sure all the key words starts with low case letters
    2. have all the variables declared
    3. check your logic is correct

    - s.s.harish
    I did what you said..I forgot and posted the un -edited code,, sorry for that,, now it will compile but not do the loop calculations.. this is where i am falling down,, all i need is direction of where to look , the logic seems to be ok..

    Thanks for you help

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    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

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Yes.
    What that's doing is making an entirely new value, and setting its value to maxJohnie / CurrentAmt. That value is then lost. Neither of those variables are changed at all. If you want to know whether the loops are working or not, put a printf statement in each one. It's a primitve version of debugging, but its always worked wonders for me.
    Code:
    void function(void)
     {
      function();
     }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Location
    Kenora Ontario
    Posts
    6

    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21