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

This is a discussion on Help with Modular code!!!!!!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; My code is supposed to give back the correct amount of change in cents(example is give me 25 cents and ...

  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,259
    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%.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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,259
    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%.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,549
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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, 10: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, 06:24 PM

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