Thread: Adding 2 Values in 'if else'

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    14

    Adding 2 Values in 'if else'

    I'm trying to write a program which prints a bill given certain input. I'm trying to add the two values for billPeak and billOff but whenever I run my program the last part gives a crazy number. All the other parts run ok. Anyone know where i'm going wrong?

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	char code;
    	int account,consump, peak, off, billPeak, billOff;
    	double bill=0;
    
    	
    	printf("Enter account no.: ");
    	scanf("%d", &account);
    	printf("Enter consumption in kwh: ");
    	scanf("%d", &consump);
    	printf("Code: ");
    	
    	while((code=getchar()) && code == '\n');
    	
    
    	if (code=='R' || code=='r' ){
    		printf("\n\nAccount no: %d\n", account);
    		bill = consump * 6.052;
    		printf("Bill %f\n", bill);
    	}
    
    
    	else if (code=='C' || code=='c' ){
    		printf("\n\nAccount no: %d\n", account);
    		if (consump<=1000)
    			bill = 60;
    		
    		else bill = (((consump-1000)*0.045)+60);
    		printf("Bill %f\n", bill);
    	}
    
    
    	else if (code=='I' || code=='i' ){
    		printf("Enter Peak: ");
    		scanf("%d", &peak);
    			if (peak<=1000)
    			billPeak = 76;
    			else billPeak = (((peak-1000)*0.045)+60);
    		printf("Enter Off Peak: ");
    		scanf("%d", &off);
    			if (off<=1000)
    			billOff = 40;
    			else billOff = (((off-1000)*0.028)+40);
    		
    		printf("\n\nAccount no: %d\n", account);
    		printf("Bill %f\n", billPeak+billOff);
    	}
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    billPeak and billOff need to be doubles too.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Missed that. Thanks. But even when I do that I keep hetting 116.000 no matter what values I enter for peak and off peak??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Using your code with the fix, I get this:
    Code:
    $ ./temp
    Enter account no.: 147
    Enter consumption in kwh: 2000
    Code: I
    Enter Peak: 1500
    Enter Off Peak: 500
    
    
    Account no: 147
    Bill 122.500000

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well 76 + 40 = 116 a lot.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    I keep getting 116. I can't figure out how yours worked ok with the same code

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Double check the code you have now vs. what you posted, and see what's different.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Because he inputted values that are both greater than 1000. Otherwise, as your program states, you get 76 + 40. You sure you wrote your own code?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Definitely wrote my own code. I've been at it for a while now and it's driving me mad. I'm just looking for some help where i'm going wrong.

    I've had a look at my code and i've changed billOff and billPeak to be doubles, put some braces into my last if/else (don't think it matters in this case). There's definitely something up in this if/else though I can't see it

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	char code;
    	double account,consump, bill, peak, off, billPeak, billOff;
    
    	
    	printf("Enter account no.: ");
    	scanf("&#37;d", &account);
    	printf("Enter consumption in kwh: ");
    	scanf("%d", &consump);
    	printf("Code: ");
    	
    	while((code=getchar()) && code == '\n');
    	
    
    	if (code=='R' || code=='r' ){
    		printf("\n\nAccount no: %d\n", account);
    		bill = consump * 6.052;
    		printf("Bill %f\n", bill);
    	}
    
    
    	else if (code=='C' || code=='c' ){
    		printf("\n\nAccount no: %d\n", account);
    		if (consump<=1000)
    			bill = 60;
    		
    		else bill = (((consump-1000)*0.045)+60);
    		printf("Bill %f\n", bill);
    	}
    
    
    	else if (code=='I' || code=='i' ){
    		printf("Enter Peak: ");
    		scanf("%d", &peak);
    			if (peak<=1000)
    			{
    			billPeak = 76;
    			}
    			else 
    			{billPeak = (((peak-1000)*0.045)+60);
    			}
    		printf("Enter Off Peak: ");
    		scanf("%d", &off);
    			if (off<=1000)
    			{billOff = 40;
    			}
    			else {billOff = (((off-1000)*0.028)+40);
    			}
    		
    
    		printf("\n\nAccount no: %d\n", account);
    		printf("Bill %f\n", billPeak+billOff);
    	}
    	return 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you type in 1500 and 500 as I did, and don't get the answer I did, then you didn't actually compile this code.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I don't know how many times I have to say it: 76 + 40 = 116.

    Well since you wrote the code you should have remembered that you hard coded

    billPeak = 76;
    and
    billOff = 40;
    In the cases where the user enters in numbers less than 1000.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. adding ASCII values
    By watshamacalit in forum C Programming
    Replies: 1
    Last Post: 12-26-2002, 07:16 PM