Creating a multi-file program

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

    Creating a multi-file program

    I am doing a multi file program and I got it to work correctly in a single file but when I split it up it isn't working properly. I keep getting errors when i compile. Can anyone help me figure out where my mistake is thanks. This is what I have so far.

    In a vendlib.h file I have

    // include file for project 3


    //converts input to product_price
    int product_price(i nt);

    //converts dollars to cents
    int dollars_cents(i nt);

    // coverts deposit to one int.
    int total_deposit(i nt);

    //converts deposit to the amount of change in cents
    int total_cents(int );

    //converts change to quarters
    int quarters(int);

    //converts change left to dimes
    int dimes(int);

    //converts change left to pennies
    int pennies(int);

    then my main.cpp file looks like this

    #include<iostre am>
    #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?" << endl;
    cin >> another;

    while (another =='y')
    {
    cout << "Enter amount of money deposited in dollars first then cents:";
    int dollars,cents;
    cin >> dollars;
    cin >> cents;

    int x,y,z;

    int product_number;
    cout << "Enter Product number.";
    cin >> product_number;

    x = product_price(p roduct_number);
    y = dollars_cents(d ollars);

    z = total_cents(xy) ;

    cout << total_cents << endl;

    int count=1;
    while (count <= 3)
    {
    int C1,C2,C3,l_c,a, b,c;
    int total;
    total = total_cents;
    a = (total/25);

    if (a > 0)
    C1=(a*25);
    if (a == 0)
    C1= 0;
    l_c=(total_cent s-C1);

    count ++;

    b = (l_c/10);

    if (b>0)
    C2=(b*10);
    if (b == 0)
    C2=0;
    l_c=(l_c - C2);

    count ++;

    c = (l_c/1);
    if (c > 0)
    C3 = c;
    if (c == 0)
    C3 = 0;

    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;
    }

    then I have a vendlib.cpp that has the def's for my functions

    /definitions file for project3
    //
    //

    #include "vendlib.h"


    //assigns the value of puchase to selection.
    int product_price(i nt)
    {

    a = (product_number == 1)
    product_price = 65;
    a = (product_number == 2)
    product_price = 116;
    a = (product_number == 3)
    product_price = 28;

    return a;
    }


    // takes input from user in dollars and converts it to cents.
    int dollars_cents(i nt)
    {
    int a = (dollars * 100);

    return a;
    }


    //takes both converted dollars and cents and puts them in one variable
    int total_deposit(i nt)
    {
    a = dollars_cents;
    b = cents;
    c = (a + b);

    return c;
    }

    // total_cents is the the amount of money left after there purchase.
    int total_cents(int ,int)
    {
    tc =(total_deposit - product_price);

    return tc;

    return b;
    }


    //quarters will take the amount after purchase and give how many quarters
    //are given back.
    int quarters(int)
    {
    int a = (total_cents/25);

    return a;
    }

    //dimes wil take the amount left after quarters and give how many dimes
    //are given back.
    int dimes(int)
    {
    int a = (l_c/10);

    return a;
    }

    //penny will take anything that is left after quarters and dimes, and
    //assign it to pennies.
    int pennies(int)
    {
    int a = l_c

    return a;
    }

    Im not sure if the problem is in my definitions or the main. I got it to work in a single file but this is the first time I have had to split up a program.
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by gdarian216
    I am doing a multi file program and I got it to work correctly in a single file but when I split it up it isn't working properly. I keep getting errors when i compile. Can anyone help me figure out where my mistake is thanks. This is what I have so far.

    In a vendlib.h file I have

    // include file for project 3


    //converts input to product_price
    int product_price(i nt);

    //converts dollars to cents
    int dollars_cents(i nt);

    // coverts deposit to one int.
    int total_deposit(i nt);

    //converts deposit to the amount of change in cents
    int total_cents(int );

    //converts change to quarters
    int quarters(int);

    //converts change left to dimes
    int dimes(int);

    //converts change left to pennies
    int pennies(int);

    then my main.cpp file looks like this

    #include<iostre am>
    #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?" << endl;
    cin >> another;

    while (another =='y')
    {
    cout << "Enter amount of money deposited in dollars first then cents:";
    int dollars,cents;
    cin >> dollars;
    cin >> cents;

    int x,y,z;

    int product_number;
    cout << "Enter Product number.";
    cin >> product_number;

    x = product_price(p roduct_number);
    y = dollars_cents(d ollars);

    z = total_cents(xy) ;

    cout << total_cents << endl;

    int count=1;
    while (count <= 3)
    {
    int C1,C2,C3,l_c,a, b,c;
    int total;
    total = total_cents;
    a = (total/25);

    if (a > 0)
    C1=(a*25);
    if (a == 0)
    C1= 0;
    l_c=(total_cent s-C1);

    count ++;

    b = (l_c/10);

    if (b>0)
    C2=(b*10);
    if (b == 0)
    C2=0;
    l_c=(l_c - C2);

    count ++;

    c = (l_c/1);
    if (c > 0)
    C3 = c;
    if (c == 0)
    C3 = 0;

    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;
    }

    then I have a vendlib.cpp that has the def's for my functions

    /definitions file for project3
    //
    //

    #include "vendlib.h"


    //assigns the value of puchase to selection.
    int product_price(i nt)
    {

    a = (product_number == 1)
    product_price = 65;
    a = (product_number == 2)
    product_price = 116;
    a = (product_number == 3)
    product_price = 28;

    return a;
    }


    // takes input from user in dollars and converts it to cents.
    int dollars_cents(i nt)
    {
    int a = (dollars * 100);

    return a;
    }


    //takes both converted dollars and cents and puts them in one variable
    int total_deposit(i nt)
    {
    a = dollars_cents;
    b = cents;
    c = (a + b);

    return c;
    }

    // total_cents is the the amount of money left after there purchase.
    int total_cents(int ,int)
    {
    tc =(total_deposit - product_price);

    return tc;

    return b;
    }


    //quarters will take the amount after purchase and give how many quarters
    //are given back.
    int quarters(int)
    {
    int a = (total_cents/25);

    return a;
    }

    //dimes wil take the amount left after quarters and give how many dimes
    //are given back.
    int dimes(int)
    {
    int a = (l_c/10);

    return a;
    }

    //penny will take anything that is left after quarters and dimes, and
    //assign it to pennies.
    int pennies(int)
    {
    int a = l_c

    return a;
    }

    Im not sure if the problem is in my definitions or the main. I got it to work in a single file but this is the first time I have had to split up a program.
    There are several things you need to change.

    First, your functions need parameter declarations. If you define a function like this:
    Code:
    int quarters(int)
    {
            int a = (total_cents/25);
    
            return a;
    }
    How (via which name) do you access the int parameter you pass? I assume it's "total_cent s"? So you should change the function to something like
    Code:
    int quarters(int total_cents)
    {
            int a = (total_cents/25);
    
            return a;
    }
    This additionaly fixes the problem of the unknown variable total_cents.

    The second return in
    Code:
    int total_cents(int,int)
    {
            tc =(total_deposit - product_price);
    
            return tc;
    
            return b;
    }
    will never be reached. I guess it's an artifact?

    In the following fragment you even need to define more parameters:
    Code:
    int total_deposit(int)
    {
    	a = dollars_cents;
    	b = cents;
    	c = (a + b);
    
    	return c;
    }
    namely dollars_cents and cents.

    Additionally, you will have to define all variables you use, such as a,b,c. Try the following
    Code:
    int total_deposit(int dollar_cents, int cents)
    {
            int c = dollar_cents + cents;
    	return c;
    }
    You may have defined all these variables globally, but that's not obvious from your posting ... at least not for me :-)

    There are possibly some more issues, but let's start with these.

    Comment

    • gdarian216
      New Member
      • Oct 2006
      • 57

      #3
      here are the changes that I made;

      #include<iostre am>
      #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?" << endl;
      cin >> another;

      while (another =='y')
      {
      cout << "Enter amount of money deposited in dollars first then cents:";
      int dollars,cents;
      cin >> dollars;
      cin >> cents;



      int product_number;
      cout << "Enter Product number.";
      cin >> product_number;

      product_price(p roduct_number);
      dollars_cents(d ollars);
      total_deposit(d ollars_cents,ce nts);

      total_cents(tot al_deposit,prod uct_price);

      cout << total_cents << endl;

      int count=1;
      while (count <= 3)
      {
      int a,b,c,l_c;

      a = quarters(total_ cents);

      if (a > 0)
      a = (a*25);
      if (a == 0)
      a = 0;
      l_c = (total_cents - a);

      count ++;

      b = dimes(l_c);

      if (b>0)
      b = (b*10);
      if (b == 0)
      b = 0;
      l_c = (l_c - b);

      count ++;

      c = pennies(l_c);
      if (c > 0)
      c = l_c;
      if (c == 0)
      c = 0;

      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 vendlib.cpp file with my function def


      #include "vendlib.h"

      int a,b,d,e,f,tc,l_ c; i dont know if i need this or not?

      //assigns the value of puchase to selection.
      int product_price(i nt product_number)
      {

      if(product_numb er == 1)
      product_price = 65;
      if(product_numb er == 2)
      product_price = 116;
      if(product_numb er == 3)
      product_price = 28;
      a = product_price;

      return a;
      }


      // takes input from user in dollars and converts it to cents.
      int dollars_cents(i nt dollars)
      {
      int b = (dollars * 100);

      return b;
      }


      //takes both converted dollars and cents and puts them in one variable
      int total_deposit(i nt 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(tot al_deposit,prod uct_price)
      {
      tc =(total_deposit - product_price);

      return tc;

      }


      i//quarters will take the amount after purchase and give how many quarters
      //are given back.
      int quarters(total_ cents)
      {
      int d = (total_cents/25);
      l_c = (d - total_cents);
      return d;
      }

      //dimes wil take the amount left after quarters and give how many dimes
      //are given back.
      int dimes(l_c)
      {
      int e = (l_c/10);

      return e;
      }

      //penny will take anything that is left after quarters and dimes, and
      //assign it to pennies.
      int pennies(l_c)
      {
      int f = l_c

      return f;
      }

      this is my vendlib.h file


      //converts input to product_price
      int product_price(i nt product_number) ;

      //converts dollars to cents
      int dollars_cents(i nt dollars);

      // coverts deposit to one int.
      int total_deposit(i nt dollars_cents, int cents);

      //converts deposit to the amount of change in cents
      int total_cents(int ,int);

      //converts change to quarters
      int quarters(int);

      //converts change left to dimes
      int dimes(int);

      //converts change left to pennies
      int pennies(int);

      these are the errors im getting if they help at all

      main.cpp: In function `int main()':
      main.cpp:31: error: invalid conversion from `int (*)(int)' to `int'
      main.cpp:31: error: initializing argument 1 of `int total_deposit(i nt, int)'
      main.cpp:33: error: invalid conversion from `int (*)(int, int)' to `int'
      main.cpp:33: error: initializing argument 1 of `int total_cents(int , int)'
      main.cpp:33: error: invalid conversion from `int (*)(int)' to `int'
      main.cpp:33: error: initializing argument 2 of `int total_cents(int , int)'
      main.cpp:35: warning: the address of `int total_cents(int , int)', will always evaluate as `true'
      main.cpp:42: error: invalid conversion from `int (*)(int, int)' to `int'
      main.cpp:42: error: initializing argument 1 of `int quarters(int)'
      main.cpp:48: error: pointer to a function used in arithmetic
      main.cpp:48: error: invalid conversion from `int (*)(int, int)' to `int'

      I got rid of a lot of the errors but I cant get these to go away

      Comment

      • gdarian216
        New Member
        • Oct 2006
        • 57

        #4
        okay i got rid of all the errors from before and this came up. I have no idea what it means. If anyone has a clue please let me know.

        /tmp/ccCOmPOx.o(.tex t+0x215): In function `main':
        : undefined reference to `product_price( int)'
        /tmp/ccCOmPOx.o(.tex t+0x226): In function `main':
        : undefined reference to `dollars_cents( int)'
        /tmp/ccCOmPOx.o(.tex t+0x23a): In function `main':
        : undefined reference to `total_deposit( int, int)'
        /tmp/ccCOmPOx.o(.tex t+0x24e): In function `main':
        : undefined reference to `total_cents(in t, int)'
        /tmp/ccCOmPOx.o(.tex t+0x283): In function `main':
        : undefined reference to `quarters(int)'
        /tmp/ccCOmPOx.o(.tex t+0x2cd): In function `main':
        : undefined reference to `dimes(int)'
        /tmp/ccCOmPOx.o(.tex t+0x311): In function `main':
        : undefined reference to `pennies(int)'
        collect2: ld returned 1 exit status

        Comment

        • arne
          Recognized Expert Contributor
          • Oct 2006
          • 315

          #5
          Originally posted by gdarian216
          okay i got rid of all the errors from before and this came up. I have no idea what it means. If anyone has a clue please let me know.

          /tmp/ccCOmPOx.o(.tex t+0x215): In function `main':
          : undefined reference to `product_price( int)'
          /tmp/ccCOmPOx.o(.tex t+0x226): In function `main':
          : undefined reference to `dollars_cents( int)'
          /tmp/ccCOmPOx.o(.tex t+0x23a): In function `main':
          : undefined reference to `total_deposit( int, int)'
          /tmp/ccCOmPOx.o(.tex t+0x24e): In function `main':
          : undefined reference to `total_cents(in t, int)'
          /tmp/ccCOmPOx.o(.tex t+0x283): In function `main':
          : undefined reference to `quarters(int)'
          /tmp/ccCOmPOx.o(.tex t+0x2cd): In function `main':
          : undefined reference to `dimes(int)'
          /tmp/ccCOmPOx.o(.tex t+0x311): In function `main':
          : undefined reference to `pennies(int)'
          collect2: ld returned 1 exit status

          These errors indicate that your main program doesn't know where to find the definitions of these functions. It seems that you compiled main.cpp without compiling/linking your vendlib.cpp file.

          Try the following:

          1. Compile vendlib.cpp alone. There are some more minor errors to correct.

          2. As soon as your lib compiles fine, link it with your main file.

          How you do that depends on the environment you use. Under Linux/g++ you could something like:

          g++ -c vendlib.cpp (compiles the lib file, creates an object file vendlib.o)

          g++ -o prog main.cpp vendlib.cpp (compiles all cpp files and links them with the required libraries to an executable called prog)

          Comment

          Working...