Thread: Help with Modular code!!!!!!!!!!!!!

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Help with Modular code!!!!!!!!!!!!!

    My code is supposed to give back the correct amount of change in cents(example is give me 25 cents and it prints out 1 twenty cent coin 5 cent) but it keeps coming up with random numbers, need help!!!!


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <ctype.h>
    
    
    char Displaybanner(Help)
    {
        int fifty, twenty, ten, five, result;
        char option;
     
        printf("\Hello Bank teller.");  
        do
        {   
            printf("\n\nDo you need help (Y/N)");
            option = getch();
            option = toupper (option);
        }
        while (!((option=='Y') || (option=='N'))); 
           
        printf("%c\n",option);
        
        if (option=='Y')
                 
            printf("\nEnter the amount of cents that needs to be returned to the customer: ");
            scanf("%d%*c", &result);
    
        return result;
                
    }
    
    
    
         
    int Calculate()
       
        {
        int fifty, twenty, ten, five, result;
                             
                if ((result<0) || (result>95) || (result%5>0))
                                  
                            
                    fifty = result / 50;
                    result %= 50;
                    twenty = result / 20;
                    result %= 20;
                        ten = result / 10;
                        result %= 10;
                    five = result / 5;
                    result %= 5; 
                 
                    printf("To make change for the amount you entered, you will need the following coins:\n\n", result);
                    
                    if (fifty > 1)
                            printf("%d Fifty cent piece's.\n", fifty);
                    if (fifty == 1)
                        printf("%d Fifty cent piece.\n", fifty);
                    if (twenty > 1)
                            printf("%d Twenty cent piece's.\n", twenty);
                    if (twenty == 1)
                            printf("%d Twenty cent piece.\n", twenty);
                    if (ten > 1)
                            printf("%d Ten cent piece's\n", ten);
                    if (ten == 1)
                            printf("%d Ten cent piece.\n", ten);
                    if (five > 1)
                            printf("%d Five cent piece's\n", five);
                    if (five == 1)
                        printf("%d Five cent piece.\n", five);
    
                        
         return result;
         }        
                   
        
            
            
            
            
            
                
                 
            
            
    int main ()
    {
         int fifty, twenty, ten, five, result;
    
        result = Displaybanner();    
        
        Calculate(); 
        }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The variable "result" inside calculate() is not initialised, hence the garbage values.

    There is no inherent relationship between result in main(), result in DisplayBanner(), or result in Calculate(). They are all distinct variables, with different addresses in memory, able to hold distinct values, etc etc. They have the same name, but different context or scope (inside their respective functions).

    The same goes for other variables.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is this really supposed to be C++? It looks only like C. Did you post in the wrong section?
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    Is this really supposed to be C++? It looks only like C. Did you post in the wrong section?
    It's not strictly C either, given the usage of <conio.h> and getch() - neither of which are standard C. But I agree, in terms of library functions used (printf(), scanf()) it is closer to being C than C++.

    The scoping concern affecting the OP is the same in both C and C++, so it probably doesn't make much difference what forum it is in.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by grumpy View Post
    The scoping concern affecting the OP is the same in both C and C++, so it probably doesn't make much difference what forum it is in.
    True, but knowing whether it is C or C++ will help us give proper advice. There is more than just scoping issues if it's supposed to be C++, as implied by the forum category.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is my code considered written in Modular style? :)
    By GangNam in forum C Programming
    Replies: 6
    Last Post: 08-26-2012, 01:20 AM
  2. Replies: 10
    Last Post: 03-08-2010, 11:30 AM
  3. Modular Programming
    By St0rM-MaN in forum C Programming
    Replies: 7
    Last Post: 05-10-2007, 02:56 AM
  4. non-modular to modular program conversion
    By strife in forum C Programming
    Replies: 3
    Last Post: 09-22-2006, 03:13 AM
  5. Modular
    By loopshot in forum Game Programming
    Replies: 7
    Last Post: 01-20-2006, 07:24 PM