Connecting Tech Pros Worldwide Help | Site Map

Creating a multi-file program

Member
 
Join Date: Oct 2006
Posts: 57
#1: Oct 20 '06
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(int);

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

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

//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<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?" << 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(product_number);
y = dollars_cents(dollars);

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_cents-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(int)
{

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(int)
{
int a = (dollars * 100);

return a;
}


//takes both converted dollars and cents and puts them in one variable
int total_deposit(int)
{
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's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#2: Oct 20 '06

re: Creating a multi-file program


Quote:

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(int);

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

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

//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<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?" << 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(product_number);
y = dollars_cents(dollars);

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_cents-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(int)
{

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(int)
{
int a = (dollars * 100);

return a;
}


//takes both converted dollars and cents and puts them in one variable
int total_deposit(int)
{
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:
Expand|Select|Wrap|Line Numbers
  1. int quarters(int)
  2. {
  3.         int a = (total_cents/25);
  4.  
  5.         return a;
  6. }
  7.  
How (via which name) do you access the int parameter you pass? I assume it's "total_cents"? So you should change the function to something like
Expand|Select|Wrap|Line Numbers
  1. int quarters(int total_cents)
  2. {
  3.         int a = (total_cents/25);
  4.  
  5.         return a;
  6. }
  7.  
This additionaly fixes the problem of the unknown variable total_cents.

The second return in
Expand|Select|Wrap|Line Numbers
  1. int total_cents(int,int)
  2. {
  3.         tc =(total_deposit - product_price);
  4.  
  5.         return tc;
  6.  
  7.         return b;
  8. }
  9.  
will never be reached. I guess it's an artifact?

In the following fragment you even need to define more parameters:
Expand|Select|Wrap|Line Numbers
  1. int total_deposit(int)
  2. {
  3.     a = dollars_cents;
  4.     b = cents;
  5.     c = (a + b);
  6.  
  7.     return c;
  8. }
  9.  
namely dollars_cents and cents.

Additionally, you will have to define all variables you use, such as a,b,c. Try the following
Expand|Select|Wrap|Line Numbers
  1. int total_deposit(int dollar_cents, int cents)
  2. {
  3.         int c = dollar_cents + cents;
  4.     return c;
  5. }
  6.  
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.
Member
 
Join Date: Oct 2006
Posts: 57
#3: Oct 21 '06

re: Creating a multi-file program


here are the changes that I made;

#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?" << 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(product_number);
dollars_cents(dollars);
total_deposit(dollars_cents,cents);

total_cents(total_deposit,product_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(int product_number)
{

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

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(total_deposit,product_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(int product_number);

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

// coverts deposit to one int.
int total_deposit(int 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(int, 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
Member
 
Join Date: Oct 2006
Posts: 57
#4: Oct 21 '06

re: Creating a multi-file program


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(.text+0x215): In function `main':
: undefined reference to `product_price(int)'
/tmp/ccCOmPOx.o(.text+0x226): In function `main':
: undefined reference to `dollars_cents(int)'
/tmp/ccCOmPOx.o(.text+0x23a): In function `main':
: undefined reference to `total_deposit(int, int)'
/tmp/ccCOmPOx.o(.text+0x24e): In function `main':
: undefined reference to `total_cents(int, int)'
/tmp/ccCOmPOx.o(.text+0x283): In function `main':
: undefined reference to `quarters(int)'
/tmp/ccCOmPOx.o(.text+0x2cd): In function `main':
: undefined reference to `dimes(int)'
/tmp/ccCOmPOx.o(.text+0x311): In function `main':
: undefined reference to `pennies(int)'
collect2: ld returned 1 exit status
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#5: Oct 21 '06

re: Creating a multi-file program


Quote:

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(.text+0x215): In function `main':
: undefined reference to `product_price(int)'
/tmp/ccCOmPOx.o(.text+0x226): In function `main':
: undefined reference to `dollars_cents(int)'
/tmp/ccCOmPOx.o(.text+0x23a): In function `main':
: undefined reference to `total_deposit(int, int)'
/tmp/ccCOmPOx.o(.text+0x24e): In function `main':
: undefined reference to `total_cents(int, int)'
/tmp/ccCOmPOx.o(.text+0x283): In function `main':
: undefined reference to `quarters(int)'
/tmp/ccCOmPOx.o(.text+0x2cd): In function `main':
: undefined reference to `dimes(int)'
/tmp/ccCOmPOx.o(.text+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)
Reply