Thread: if/else and printf questions

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    if/else and printf questions

    I can't seem to figure out why my if / else series will not print. When I compile I get warnings, but no errors. It will let me run the program but stops after returning total hours. All help and suggestions are greatly appreciated.


    Code:
    #include<stdio.h>
    
    #define low 5.151
    #define medium 7.110
    #define high 9.800
    
    int getUserInput ( );
    int getTotalKWH (end_mtr, start_mtr);
    float calculateTheBill (bill);
    
    int main ( )       
    {
    		int cust_num = 0;
    		int start_mtr = 0;
    		int end_mtr = 0;
    		int hours = 0;
    		float bill = 0;
    
    		printf ("This program will compute BetterJoules Electric Company Customer Bills");
    
    		printf ("\n\nPlease enter Customer Number: ");
    		
    			cust_num = getUserInput ( );
    
    		printf ("\n\nPlease enter starting meter reading:  ");
    		
    			start_mtr = getUserInput ( );
    
    		printf ("\n\nPlease enter ending meter reading:  ");
    		
    			end_mtr = getUserInput ( );
    
    			hours = getTotalKWH (end_mtr, start_mtr);
    
    			bill = calculateTheBill (hours, cust_num, end_mtr, hours, bill );
    
    		return (0);		
    
    }
    
    int getUserInput ( )
    {
    		int input = 0;
    
    		scanf ("%d%*c", &input);
    
    		return input;
    
    }
    
    int getTotalKWH (int end_mtr, int start_mtr)
    {
    		int hours = 0;
    
    		hours = end_mtr - start_mtr;
    
    		printf("\n\nTotal Kilowatt Hours used: %d ", hours);
    
    		scanf ("%d%*c", &hours);
    
    		return hours;
    
    }
    float calculateTheBill (int cust_num, int hours, int end_mtr, int start_mtr)
    {
    		float bill = 0;
    
    		
    			if( hours <= 300 ) {
    				bill == (hours*low)/100;
    				scanf ("%.2f", &bill);
    			}
    			else if( hours <= 500 ) {
    				bill == (300*low + (end_mtr - 301)*medium)/100;
    				scanf ("%.2f", &bill);	
    			}
    			else if (hours > 500) {
    				bill == (300*low + 199*medium + (end_mtr -501)*high)/100;
    				scanf ("%.2f", &bill);
    			}
    
    		printf ("\n\n Cusomer %d's total bill is $%.2f", cust_num, bill);
    		
    		return bill;
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am guessing that in calculateTheBill() you want to compute the bill and store it in bill rather than compare bill to the result of some computation, and then read in into bill.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if( hours <= 300 ) {
    				bill == (hours*low)/100;
    				scanf ("%.2f", &bill);
    			}
    What do you think this is supposed to do? == does not do assigning, only comparing, and of course scanf doesn't print anything out at all.

    Also, you pass your arguments to calculateTheBill in the wrong order.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Quote Originally Posted by laserlight View Post
    I am guessing that in calculateTheBill() you want to compute the bill and store it in bill rather than compare bill to the result of some computation, and then read in into bill.
    yes, that is what I am looking to do. i was very happy when I compiled and I got no errors. I thought I had done something correct. this stuff is eating my lunch. thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM