Thread: My program needs troubleshooting...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    My program needs troubleshooting...

    Well, I tried using the debugger, but I still don't get what I'm doing wrong. Basically, this program is supposed to simulate how much making a long distance call would cost. However, I keep getting a very messed up value. I assume it has something to do with the functions. I'm not sure though. Anyone want to help me out? Thanks


    Code:
    /*Lab B*/
    
    #include <stdio.h>
    
    int get_gross_cost(int);
    int get_net_cost(int, int);
    int main() 
    
    {
    //Declarations
    float discount_rate, regular_rate, tax, net_cost, gross_cost;
    int minutes, start_time;
    char reply;
    
    //Explanation
    printf("This program is designed to calculate how much a long distance call between it's");
    printf("peak hours");	
    
    do
    {
    //User input
    printf("\n\nPlease enter what time you would like a call to start (0001-2400): ");
    scanf("%i", &start_time);
    
    printf("\nPlease enter the duration of the call in minutes: ");
    scanf("%i", &minutes);
    
    //Functions
    get_gross_cost(minutes);
    
    get_net_cost(minutes, start_time);
    
    printf("The gross cost of your call is %f", gross_cost);
    printf("\nThe net cost of your call is %f", net_cost);
    
    printf("\n\n\nWould you like to try again? <y/n>");
    scanf("\n%c", &reply);
    }
    while(reply == 'y' || reply == 'Y');
    
    return 0;
    }
    
    
    
    int get_gross_cost(minutes)
    
    {
    float gross_cost;
    float regular_rate=0.40;
    
    gross_cost=minutes*regular_rate;
    
    return gross_cost;
    }
    
    
    int get_net_cost(start_time, minutes)
    
    {
    
    float regular_rate;
    float net_cost;
    int tax;
    
    if (1800<start_time || start_time<800)
    
    {
    net_cost=(minutes*regular_rate/2);
    tax=(net_cost*.04);
    }
    
    else
    {
    float net_cost=minutes;
    }
    
    return net_cost;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Indent the code so people will actually read it. Then try to give us a better idea of what is wrong.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Declarations or function prototypes should be in a header or at the beginning of your source file, NOT inside functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just because variables have the same name in functions and in main doesn't mean they are the same variables.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Just because variables have the same name in functions and in main doesn't mean they are the same variables.
    Are you talking about gross_cost and net_cost? I'm missing something vital here, and it seems to be whats wrong with most of my programs. Something I'm not grasping.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    On a side note you will get better decimal percision with float by using .2

    Code:
    printf("My decimal value is: &#37;.2f\n", decimal);
    Double Helix STL

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Rockiroad278 View Post
    Are you talking about gross_cost and net_cost? I'm missing something vital here, and it seems to be whats wrong with most of my programs. Something I'm not grasping.
    Yes. Every function gets its own "scope", so that variables declared in one function are separate from every other function (and since main is a function, that's included too). So gross_cost in main and gross_cost in get_gross_cost aren't the same thing.

    Illustrative example:

    Code:
    int add(int a, int b) {
        int c;
        c = a+b;
        return c;
    }
    
    int main() {
        int a = 2, b = 3, c = 4, d = 5;
        add(a,d);
        return 0;
    }
    The add function call will return 7, and c will still be 4, since the c in add is not the same as the c in main. The variable b in main is never used at all. If you want to use the value of 7 in main, you have to assign it to a variable (perhaps with c=add(a,d)).

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    
    float get_gross_cost(int);
    float get_net_cost(int, int);
    
    int main() 
    {
        //Declarations
        float discount_rate, regular_rate, tax, net_cost, gross_cost;
        int minutes, start_time;
        char reply;
        
        //Explanation
        printf("This program is designed to calculate how much a long distance call between it's");
        printf("peak hours");	
        
        do
        {
            //User input
            printf("\n\nPlease enter what time you would like a call to start (0001-2400): ");
            scanf("&#37;i", &start_time);
            
            printf("\nPlease enter the duration of the call in minutes: ");
            scanf("%i", &minutes);
            
            //Functions
            gross_cost = get_gross_cost(minutes);
            
            net_cost = get_net_cost(minutes, start_time);
            
            printf("The gross cost of your call is %f", gross_cost);
            printf("\nThe net cost of your call is %f", net_cost);
            
            printf("\n\n\nWould you like to try again? <y/n>");
            scanf("\n%c", &reply);
        }
        while(reply == 'y' || reply == 'Y');
        
        return 0;
    }
    
    float get_gross_cost(int minutes)
    {
        float gross_cost;
        float regular_rate=0.40;
        
        gross_cost=minutes*regular_rate;
        
        return gross_cost;
    }
    
    int get_net_cost(int start_time,int  minutes)
    {
    
        float regular_rate;
        float net_cost;
        int tax;
        
        if (1800<start_time || start_time<800)
        {
            net_cost=(minutes*regular_rate/2);
            tax=(net_cost*.04);
        }
        else
        {
            float net_cost=minutes;
        }
        
        return net_cost;
    }
    This is how the indented code looks like, u should start indenting you code, so that u will get more help. And now have look at the changes which has been node on your code.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM