Help with calculation and loop

This is a discussion on Help with calculation and loop within the C Programming forums, part of the General Programming Boards category; Hi - I'm haveing problems with the calculation and loop. Here are my problems: Calculation - When I enter in ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    Help with calculation and loop

    Hi - I'm haveing problems with the calculation and loop. Here are my problems:
    Calculation - When I enter in a start time and a length time the calculation is not coming out correctly and I can't seem to figure out why.
    Loop - I had to write a line to say that you may enter another set by hitting the n key. I cannot figure out how to make it so that it automatically goes to the next set without hitting the n key.
    Any help is greatly appreciated. Here is what I have coded so far.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define rate .10
    #define discounta .50
    #define discountb .15
    #define tax .04
    
    void getStartTime(int time);
    void getLengthofCall(int length);
    void calcGross(int time, int length, double gross);
    void calcNet(int time, int length,double gross, double net);
    void displayReport(double gross, double net, char input);
    void end();
    
    int main()
    {
        int time;
        int length;
        double gross;
        double net;
    	char input;
        
        getStartTime(time);
        getLengthofCall(length);
        calcGross(time, length, gross);
        calcNet(time, length, gross, net);
        displayReport(gross, net, input);
    	return 0;
    }
    
    void getStartTime(int time)
    {
         printf("Please input start time: ");
         scanf("%d", &time);
    	 if(time >= 2401 || time <= -1)
    	 {
    		 printf("Invalid time. Please input a time between 0 and 2400.\n\n");
    		 main();
    	 }
    }
    
    void getLengthofCall(int length)
    {
         printf("Please input call length: ");
         scanf("%d", &length);
    }
    
    void calcGross(int time, int length, double gross)
    {
        if(time <= 800 || time >= 1800 && time <= 60)
        {
            gross = length*rate*discounta;
        }
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
            gross = length*rate*discounta*discountb;
        }
        else
        {
            gross = length*rate;      
        }
    }
    
    void calcNet(int time, int length, double gross, double net)
    {
        if(time <= 800 || time >= 1800 && time <= 60)
        {
            net = gross*tax+gross;
        }
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
            net = gross*tax+gross;
        }
        else
        {
            net = gross*tax+gross;       
        }
    }
    void displayReport(double gross, double net, char input)
    {
         printf("\nGross: %lf\nNet: %lf", gross, net);
    	 printf("\nYou may enter another set by hitting the n key.\n");
    	 printf("\nYou may exit this program by hitting the e key.\n");
    	 scanf("%s", &input);
    	 if (input!='e')
    	 {
    	 	main();
    	 }
    	 else if(input='e')
    	 {
    		 end();
    	 }
    }
    void end()
    {
         printf("\n\nThank you for using this phone calculator program!\n");
    }

    This is my output
    [code
    Please input start time: 1600
    Please input call length: 15

    Gross: -92559631349317831000000000000000000000000000000000 000000000000.000000
    Net: -92559631349317831000000000000000000000000000000000 000000000000.000000
    You may enter another set by hitting the n key.

    You may exit this program by hitting the e key.
    [/code]

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    11

    Sorry

    Sorry about any typo errors and posting errors - I'm new to the site.

    Thanks
    Betty

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Two major problems with your code.

    The first is your fuctions: they don't return any data.
    Code:
    void getStartTime(int time)
    That one has this problem, though it occurs in other functions too. C has what is called pass by value, so when in function main() you say getStartTime(time), you're passing the value of time, not the variable itself. It's' prefectly legal to modify a variable that was passed to a function in the function like, but they're not premenent. Example:
    Code:
    void MyFunc(int x)
    {
      x = 3;
    }
    int main()
    {
      int y = 2;
      MyFunc(y);
      printf("y = %d", y); // This prints "y = 2"
      return 0;
    }
    So, how to get functions to return information? You've got two choices: A function can return a value, or you can use pointers. Example:

    Code:
    int getStartTime()
    {
      int time;
      // Prompt user for info, etc, etc.
      return time;
    }
    int main()
    {
      int time;
      time = getStartTime();
      return 0;
    }
    Pointers are a bit more advanced, and I wouldn't yet recommend them. If you want to see examples though, check out the FAQs.

    Second: In calcGross() (The other calc function has this same problem) you have:
    Code:
    if(time <= 800 || time >= 1800 && time <= 60)
    Which will only prove true when time <= 800. The && operator occurs before the || operator, so you get "is time >= 1800 AND <= 60?" The answer is always no. So only the time <= 800 side can cause it to be true. (Turn warnings on, the compiler might tell you.)

    Turn on warnings! Both errors might throw warnings. (The first because you're using uninitialized variables.)
    Hope that helps!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Thanks you so much for your response. It appears that it's not storing the values correctly. Any other suggestions are greatly appreciated.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define rate .10
    #define discounta .50
    #define discountb .15
    #define tax .04
    
    int getStartTime();
    int getLengthofCall();
    double calcGross();
    double calcNet();
    void displayReport();
    void end();
    
    int main()
    {
        int time=0;
        int length=0;
        double gross=0;
        double net=0;
    	char input=0;
    
        
        getStartTime();
        getLengthofCall();
        calcGross();
        calcNet();
        displayReport();
    	return 0;
    }
    
    int getStartTime()
    {
         int time; 
    	 
    	 printf("Harford Telephone Company\n");
     	 printf("\nStart time:       ");
         scanf("%d", &time);
    	 if(time >= 2401 || time <= -1)
    	 {
    	 printf("Invalid time. Please input a time between 0 and 2400.\n\n");
    	 getStartTime();
    	 }
    	 return time;
    }
    
    int getLengthofCall()
    {
    	 int length;
    
         printf("Length of call:   ");
         scanf("%d", &length);
    	 
    	 return length;
    }
    
    double calcGross()
    {
        int time, length;
    	double gross;
    
    	if(time <= 800 || time >= 1800 && time <= 60)
        {
            gross = length*rate*discounta;
    	}
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
            gross = length*rate*discounta*discountb;
        }
        else
        {
            gross = length*rate;      
        }
    	return gross;
    }
    
    double calcNet()
    {
    	int time, length;
    	double gross, net;
    
        if(time <= 800 || time >= 1800 && time <= 60)
        {
            net = gross*tax+gross;
        }
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
           net = gross*tax+gross;
        }
        else
        {
            net = gross*tax+gross;       
        }
    	return net;
    }
    
    void displayReport()
    {
    	 double gross, net;
    	 char input;
    
         printf("\nGross: %.2f", gross);
    	 printf ("\nNet: %.2f", net);
    	 printf("\nYou may enter another set by hitting the n key, than enter.\n");
    	 printf("\nYou may exit this program by hitting the e key.\n");
    	 scanf("%s", &input);
    	 if (input!='e')
    	 {
    	 	main();
    	 }
    	 else if(input=='e')
    	 {
    		 end();
    	 }
    }
    
    void end()
    {
         printf("\n\nThank you for using this phone calculator program!\n");
    }
    
    
    void end()
    {
         printf("\n\nThank you for using this phone calculator program!\n");
    }

    This is what I'm getting with the suggested changes
    Code:
    Start time:       1600
    Length of call:   15
    
    Gross: -92559631349317831000000000000000000000000000000000000000000000.00
    Net: -92559631349317831000000000000000000000000000000000000000000000.00

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Imagine you are a computer.
    Code:
    double calcNet()
    {
    	int time, length;
    	double gross, net;
    
        if(time <= 800 || time >= 1800 && time <= 60)
        {
            net = gross*tax+gross;
        }
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
           net = gross*tax+gross;
        }
        else
        {
            net = gross*tax+gross;       
        }
    	return net;
    }
    What is the value of time (if that were a variable name you could actually use) when you use it?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Dave, I started to correct the code because I can't sleep. Here is what I did so far

    Code:
    #include <stdio.h>
    
    #define RATE .10
    #define DISCA .50
    #define DISCB .15
    #define TAX .04
    
    int getStartTime();
    int getLengthofCall();
    double calcGross();
    double calcNet();
    void displayReport();
    void end();
    
    int main(void)
    {
        int start_time = 0;
        int call_length = 0;
        double gross = 0;    
        double net = 0;
    
        start_time = getStartTime();
        call_length = getLengthofCall();
    
        gross=calcGross(start_time, call_length);
        net = calcNet(start_time, call_length, gross);
        displayReport(gross, net);
    
        return 0;
    }
    
    int getStartTime(void)
    {
        int time; 
             
        printf("Harford Telephone Company\n");
        fputs("\nStart time:\t", stdout);
        scanf("%d", &time);
        if(time >= 2401 || time <= -1)
        {
    	printf("Invalid time. Please input a time between 0 and 2400.\n\n");
        }
        return time;
    }
    
    int getLengthofCall(void)
    {
        int length;
    
        printf("Length of call:   ");
        scanf("%d", &length);
             
        return length;
    }
    
    double calcGross(int time, int length)
    {
        double gross;
    
        if(time <= 800 || time >= 1800 && time <= 60)
        {
            gross = length*RATE*DISCA;
        }
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
            gross = length*RATE*DISCA*DISCB;
        }
        else
        {
            gross = length*RATE;      
        }
        return gross;
    }
    
    double calcNet(int time, int length, double gross)
    {
        double net;
    
        if(time <= 800 || time >= 1800 && time <= 60)
        {
            net = gross*TAX+gross;
        }
        else if(time <= 800 || time >= 1800 && time >= 60)
        {
    	net = gross*TAX+gross;
        }
        else
        {
            net = gross*TAX+gross;       
        }
        return net;
    }
    
    void displayReport(double gross, double net)
    {
        char input;
    
        printf("\nGross: %.2f", gross);
        printf ("\nNet: %.2f", net);
        printf("\nYou may enter another set by hitting the n key, than enter.\n");
        printf("\nYou may exit this program by hitting the e key.\n");
        scanf("%s", &input);
        if (input!='e')
        {
    	main();
        }
        else if(input=='e')
        {
    	end();
        }
    }
    
    void end()
    {
        printf("\n\nThank you for using this phone calculator program!\n");
    }
    $gcc -Wall phone.c -o phone
    phone.c: In function `calcGross':
    phone.c:60: warning: suggest parentheses around && within ||
    phone.c:64: warning: suggest parentheses around && within ||
    phone.c: In function `calcNet':
    phone.c:79: warning: suggest parentheses around && within ||
    phone.c:83: warning: suggest parentheses around && within ||
    $./phone
    Harford Telephone Company

    Start time: 1600
    Length of call: 60

    Gross: 6.00
    Net: 6.24
    You may enter another set by hitting the n key, than enter.

    You may exit this program by hitting the e key.
    e


    Thank you for using this phone calculator program!
    $

    I still don't know how to check scanf() for bunk values.

    Would be

  7. #7
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Here is code that I've modified.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define RATE .10
    #define DISCA .50
    #define DISCB .15
    #define TAX .04
    #define EXIT -99
    
    int getStartTime();
    int getLengthofCall();
    double calcGross();
    
    int main(void)
    {
        int start_time = 0;
        int call_length = 0;
        double gross = 0;    
        double net = 0;
        printf("\nEnter -99 as start time to exit...\n");
    		    
        while(1) { 
         start_time = getStartTime();
         if (start_time == EXIT) {
           printf("\n\nThank you for using this phone calculator program!\n");
           exit(0);  	     
         } else if (start_time < 0) {
    	     printf("\nInvalid time..Please re-enter...");
    	     continue;
         }
         call_length = getLengthofCall();
         if( call_length < 0) {
    	    printf("\nPlease enter valid length..");
    	    continue;
         }
         gross=calcGross(start_time, call_length);
         net = gross*TAX+gross;       
         printf("\nGross: %.2f", gross);
         printf ("\nNet: %.2f", net);
        }
    
        return 0;
    }
    
    int getStartTime(void)
    {
        int time; 
             
        printf("\n\nHarford Telephone Company\n");
        fputs("\nStart time:\t", stdout);
        scanf("%d", &time);
        if (time == EXIT) 
            return time;
        if(time >= 2401 || time <= 0)
    	return -1;
        return time;
    }
    
    int getLengthofCall(void)
    {
        int length;
    
        printf("Length of call:   ");
        scanf("%d", &length);
        return length;
    }
    
    double calcGross(int time, int length)
    {
        double gross;
    
        if((time <= 800 || time >= 1800) && time <= 60)
        {
            gross = length*RATE*DISCA;
        }
        else if(time >= 60)
        {
            gross = length*RATE*DISCA*DISCB;
        }
        else
        {
            gross = length*RATE;      
        }
        return gross;
    }
    U can see I've removed some functions & they are just time consuming & just printing 2-3 print lines.

    1. I've remove calcnet() as it is repeating line
    Code:
     net = gross*TAX+gross;
    for all valid time inputs,so it is need not to be in seperate function.Even U've decided to keep program modular then u can keep this single line in that function.as it is same for all conditions.

    2.The conditions
    Code:
     if((time <= 800 || time >= 1800) && time <=  60)
    are problematic. when time<=60 then & then only that condition will hold true as it is after && operator & it must hold true.To hold this condition true,
    time<=60 is sufficient condition cause as time is <= 60 then its obviously <=800 & never be >=1800 so its time consuming to check for condition (time<=800 && tme >=1800), so let me know exact conditions.

    3.This program exits when u type -99 as time,it don't require to type either 'e' or 'n'.

    4.Instead of calling main again & again I've made provision of while(1) loop & continue statements.

    5.I have removed end() as it i printing only 1 line,to make program efficient for run time.

    6.I've added code that will verify length,however I haven't set upper limit but u can do it.

    7.Line
    Code:
    gross = length*RATE;
    will not be calculated on any condition.

    Please let me know If Im mistaking somewhere.

    Thanks & Regards
    Vinit

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    11
    Thanks alot - All of your help is greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with array calculation
    By magiccity77 in forum C Programming
    Replies: 21
    Last Post: 09-29-2008, 08:44 PM
  2. Loop
    By mngt in forum C Programming
    Replies: 2
    Last Post: 04-10-2008, 03:49 AM
  3. do-while loop
    By taurus in forum C Programming
    Replies: 2
    Last Post: 09-20-2007, 06:10 PM
  4. write a loop to a file, getopt()
    By kristy in forum C Programming
    Replies: 3
    Last Post: 08-15-2003, 03:57 PM
  5. sentinel terminated loop/avg calculation
    By Sway in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2002, 08:04 AM

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