My program needs troubleshooting...

This is a discussion on My program needs troubleshooting... within the C Programming forums, part of the General Programming Boards category; Well, I tried using the debugger, but I still don't get what I'm doing wrong. Basically, this program is supposed ...

  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,211
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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,185
    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,475
    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);
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,682
    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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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