473,414 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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(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.
Oct 20 '06 #1
4 2496
arne
315 Expert 100+
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.
Oct 20 '06 #2
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
Oct 21 '06 #3
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
Oct 21 '06 #4
arne
315 Expert 100+
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)
Oct 21 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: DPhelps | last post by:
I have a multithreaded Python shell server (base on the sample code 'pysvr.py') that uses a C-based extension class. The trouble I'm having is that I cannot figure out a way to create a Python...
10
by: *ProteanThread* | last post by:
since a majority of people are still on dial up or at most DSL (though there *IS* broadband) does it help or hurt one's webpage adding multi media ? does it necessarily distract from the theme of...
0
by: Sreedharan | last post by:
Hello everyone, I am a VC++ programmer. When i develop a multi-threaded application in C++, I prefer to use worker thread, so that the main thread and worker thread can communicate using...
2
by: Dino | last post by:
I need to create a public multi-dim array that can hold a mixture of string and int values. I will need to search this array for a value and return a value from a subscript. This array will...
5
by: John | last post by:
I have 2 tables, one with dates and information about those dates, and one with people information. I want to create a report listing each date and the people who attended on that date (who have...
4
by: crescent_au | last post by:
Hi all, I'm doing some research online on creating php-based multi-level marketing (MLM) system. It seems like a complicated system to build as I need to create one from scratch. I'd like to...
5
by: Nayan | last post by:
Hi, If I make a call to function which is in external library, and it goes into wait sate.. disabling my app to proceed further, how can I break this state elegantly? So far, I had to kill my...
1
by: Nathan Sokalski | last post by:
I have a Button control whose text property is "Move To Archives". Because this makes a relatively wide Button when the text is all one line, I want to put a line break between the words "Move To"...
3
by: Nathan Sokalski | last post by:
I am looking to create a multi-column list that is scrollable, kind of like a ListBox control, only I need a way to align the columns of text that I will have (I would use spaces, but because the...
11
by: Jeff | last post by:
Hello everyone. I've searched through the archives here, and it seems that questions similar to this one have come up in the past, but I was hoping that I could pick your Pythonic brains a bit. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.