Thread: Function (modules) and loop help.

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Function (modules) and loop help.

    My name is Doug and I'm new to the forums. I am currently taking a C programming class and it's the only language I have studied besides visual basic. I am having a lot of trouble with with the current project I have been assigned and received very little help from the teaher. I was wondering if I could find some help in here! Anyway I will state the problem and what I have and be open to any suggestions. Thank you so much and i hope someone can help!

    The programming supervisor was pleased with the solution to Program 2. The supervisor would now like you to update your solution to Program 2 to develop a more robust Phone-call Cost Calculating System. Modify your solution as follows, in order to divide the program into functions, validate the input and allow the user to enter data on any number of calls. This program will include functions, loops and validation.

    1. Write functions to do the following:
    1. getStartTime()
    to input the start time from the user. If the input value is not valid, continue to request a valid start time from the user and return the valid value to main().

    2. getLengthofCall()
    to input the length of the call from the user. If the input value is not valid, continue to request a valid length of time from the user and return the valid value to main().

    3. calcGross()
    to receive the start time and length of the call. The function should calculate the gross cost and return the value to main().

    4. calcNet()
    to receive the gross cost, the start time and the length of the call. The function should calculate the net cost by applying the discounts add tax as appropriate and return the net cost to main().

    5. displayReport()
    to receive the gross cost and net cost. The function is to display the output report for the user. The function returns no value to main().

    2. Allow the user to continue entering data on calls until the user enters a sentinel for end of file. The programmer may determine the sentinel but must inform the user about how to terminate the program.

    3. At the end of the program, display a thank you message to the user for using the phone call calculator program.

    This is what I got for the same problem without the loop and functions it works perfectly! However I am not sure how to switch it into functions and apply the loop. Please help!
    Below is my code that must meet the new standards listed above.

    Code:
    //Doug Miller
    
    //Project #2 - Phone Bill Calculation Program
    
    #include <stdio.h>
    
    #include <conio.h>
    
     
    
    int main()
    
    {
    
    	// Declare Variables
    
    	int start_Time = 0;
    	int length_Of_Call = 0;
    	double gross_Cost = 0;
    	double net_Cost = 0;
    	double pre_Tax = 0;
    	double time_Discount = 0;
    	double minutes_Discount = 0;
    
    
    	//Gather Information	
    
    	printf("Please enter your call starting time (world clock): ");
    	scanf("%d", &start_Time);
    
    
    	printf("Please enter length of call: ");
    	scanf("%d", &length_Of_Call);
    
    if( start_Time > 1800 || start_Time < 800)
    {		
    	if (length_Of_Call > 60)
    		{	gross_Cost = (float)(length_Of_Call * .10);
    			pre_Tax = (gross_Cost - (gross_Cost * .50));
    			minutes_Discount = (pre_Tax - (pre_Tax * .15));
    			net_Cost = (float)(minutes_Discount + (minutes_Discount * .04));
    		}
    		else
    		{
    			gross_Cost = (float)(length_Of_Call * .10);
    			pre_Tax = (gross_Cost - (gross_Cost * .50));
    			net_Cost = (float)(pre_Tax + (pre_Tax * .04));
    		}
    }
    else if (length_Of_Call > 60)
    		{	
    			gross_Cost = (float)(length_Of_Call * .10);
    			minutes_Discount = (gross_Cost - (gross_Cost * .15));
    			net_Cost = (float)(minutes_Discount + (minutes_Discount * .04));
    		}
    
    else
    {	
    	gross_Cost = (float)(.10 * length_Of_Call);
    	net_Cost = (float)(gross_Cost + (gross_Cost * .04));
    }
    
    	printf("\n\nStart Time: %d", start_Time,"\n\n");
    	printf("\n\nLength of call: %d", length_Of_Call,"\n\n");
    	printf("\n\nGross Cost: %.2f", gross_Cost,"\n\n");
    	printf("\n\nNet Cost: %.2f", net_Cost,"\n\n");
    
    getch();
    
    return 0;
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > printf("Please enter your call starting time (world clock): ");
    > scanf("%d", &start_Time);
    Would become in main
    start_Time = getStartTime();

    The prototype (put this before you try and call it) would be
    int getStartTime ( void );


    And that function would be
    Code:
    int getStartTime ( void ) {
            int timeNow;  /* Just a different name, just to show it can be different */
            /* simply copy/pasting start_Time would work just as well */
    	printf("Please enter your call starting time (world clock): ");
    	scanf("%d", &timeNow);
            return timeNow;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Thanks that make sense but I don't totally understand the (void) . Also for the loop could I just make it run until the user entered zero, and then tell it to cut the program off?

    keep everything but the start_time module in the loop. Is this what she means by the sentinel value. It would seem you would need the user to end the loop since they could input an indefinite number of calls.


    Code:
    while start_time != 0

    and preform the other modules under that? Also for the more complex modules would I need to write the if conditions for each module? It seems a bit pointless that the teacher wants two separate modules for the gross and net cost...

    P.S. Thanks so much for the help you've already sent. More than my teacher did... I have to go to school though so I hope to hear from you guys later. Thanks.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Guess no more advice? lol

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Using void as an argument in a function definition (ie. in the prototype) means that the function is defined to accept no parameters.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Guess no more advice? lol
    No, I gave you an answer for one of them.

    It's up to you to "lather, rinse and repeat" your way through all the other ones, with your new gained knowledge of the problem.

    Or at least post some kind of code which showed you at least tried to do that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM