Question why this is happening in a multi file program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    Question why this is happening in a multi file program

    I am writing a multifile program and it worked when it was all in one file. I have gotten all of the errors out of the program except when I go to output the change the quarters are right but the dimes and pennies print out a long string of numbers. I can't figure out were these numbers are coming from. If anyone can help I will be appreciated.

    This is my main.cpp code for the body of my program.

    Code:
    #include<iostream>
    #include"vendlib.h"
    using namespace std;
    
    int main()
    {
    
    char another;
    
    cout << "Welcome to online vendiing machine." << endl;
    cout << "Product list: 1. M&M($0.65) 2. Chips ($1.16) 3. Pepermint gum ($0.28)."<< endl;
    
    cout << "would you like to make a purchase?(y/n)" << endl;
    cin >> another;
    int dollars,cents;
    
    while (another =='y')
    {
    cout << "Enter amount of money deposited in dollars first then cents:";
    cout << "dollars";
    cin >> dollars;
    cout << "cents";
    cin >> cents;
    
    
    int product_number;
    cout << "Enter Product number.";
    cin >> product_number;
    
    int a,b,c,d;
    a = product_price(product_number);
    b = dollars_cents(dollars);
    c = total_deposit(b,cents);
    
    d = total_cents(c,a);
    cout << d << endl;
    
    int count=1;
    while (count <= 3)
    {
    int a,b,c,C1,C2;
    
    a = quarters(d);
    count++;
    b = dimes(d,C1)
    count++;
    c = pennies(d,C1,C2);
    count++;
    
    cout << "Your change is";
    cout << a  << "quarters " <<" " <<  b << "dimes " <<" " <<"and "<< c <<"pennies" <<  endl;
     }cout << "would you like another purchase. (y/n)" << endl;
    cin >> another;
    }
    
    return 0;
    }
    This is my code to my vendlib.cpp file

    Code:
    
    #include "vendlib.h"
    
    int a,b,d,e,f,tc,C1,C2,C3;
    
    //assigns the value of puchase to selection.
    int product_price(int product_number)
    {
    
            if(product_number == 1)
            {
                     a = 65;
            }
    
            if(product_number == 2)
            {
                    a = 116;
            }
    
            if(product_number == 3)
            {
                    a = 28;
            }
    
            return a;
    }
    // takes input from user in dollars and converts it to cents.
    int dollars_cents(int dollars)
    {
            int b = (dollars * 100);
    
            return b;
    }
    
    
    //takes both converted dollars and cents and puts them in one variable
    int total_deposit(int dollars_cents, int cents)
    {
            int c = dollars_cents + cents;
    
    
    
            return c;
    }
    
    // total_cents is the the amount of money left after there purchase.
    int total_cents(int total_deposit, int product_price)
    {
            tc =(total_deposit - product_price);
    
            return tc;
    
    }
    
    
    //quarters will take the amount after purchase and give how many quarters
    //are given back.
    int quarters(int total_cents)
    {
            d = (total_cents/25);
            if (d > 0)
                    C1 = (d);
            if (d == 0)
                    C1 = 0;
            return d;
    }
    
    //dimes wil take the amount left after quarters and give how many dimes
    //are given back.
    int dimes(int total_cents,int C1)
    {
            e  = ((total_cents - C1)/10);
            if (e > 0)
                    C2 = (e*10);
            if (b == 0)
                    C2 = 0;
            return e;
    }
    
    //penny will take anything that is left after quarters and dimes, and
    //assign it to pennies.
    int pennies(int total_cents,int C1,int C2)
    {
            f = ((total_cents - C1 - C2)/1);
            if (f > 0)
                    C3 = f;
            if (f == 0)
                    C3 = 0;
    
            return f;
    }

    this is a sample of the output when the program is run.

    [PHP]Welcome to online vendiing machine.
    Product list: 1. M&M($0.65) 2. Chips ($1.16) 3. Pepermint gum ($0.28).
    would you like to make a purchase?(y/n)
    y
    Enter amount of money deposited in dollars first then cents:dollars2
    cents50
    Enter Product number.1
    185
    134521512
    134516300
    134516477
    Your change is7quarters -13452132dimes and -269037627pennie s
    would you like another purchase. (y/n)
    n[/PHP]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In main

    Code:
    int a,b,c,C1,C2;
    
    a = quarters(d);
    count++;
    b = dimes(d,C1)
    count++;
    c = pennies(d,C1,C2);
    count++;
    You use C1 and C2 without initialising them to anything

    in vendlib.cpp
    Code:
    int a,b,d,e,f,tc,C1,C2,C3;
    Having global variables like this is very very poor style (especially given that they don't have meaningful names).

    Remove this line and fix compile errors by creating local variables in the functions themselves. You will then find that you also have some unreferenced variables (an unreferenced variable is 1 that is declared and may be assign to but the value of it is never used), you compiler may or may not warn you about these so check the code for variables whos values are not used.

    Looking at this program there is no need to it to have any global data.

    Comment

    Working...