![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 2
Rep Power: 0
![]() |
problem with user defined class mixed with functions
Hi, I am a beginner in C++ programming. Not only am I new to the language, I only recently registered with this website; so please bear with me.
My question is rather lengthy: I am trying to create a program for my college C++ class that keeps an inventory for a very simple convenience store. It only sells 5 things: gas, beer, soda pop, candy and chips. Each item has its own price. My job is to write an inventory tracking program so that as a customer comes through, I can track how much a customer owes and track home much inventory I have. I need to: 1. Print the initial inventory. Printing of the inventory should be done in tabular format, with all items lining up in columns. 2. Prompt the user for the quantity of a product to buy. The program will print the price of the item to the user. 3. Continue prompting until the user finishes. The program will print the updated inventory. I also need to: create a class "inventory", which contains the quantity of each of the five items (as an integer) and the price of each of the five items (as a double). The requirements are basically to have a user defined class, use a switch statement somewhere in the code, and to have 7 functions. The functions purposes and their declarations are: 1. Inventory int(int,double,int,double,int,double,int,double,int,double). This function takes in the initial quantities and the price for each of the 5 items. It returns an Inventory. 2. void report (Inventory). This function provides a report of the present inventory. This includes the quantity and price of each item and the total worth of the present inventory. 3. Inventory gas (Inventory, int). This function takes in an Inventory and a quantity of gas. It then provides a price message to the customer and updates the Inventory. It returns the updated Inventory. 4. Inventory pop (Inventory, int). Same as above but for soda pop. 5. Inventory beer (Inventory, int). Same as above but for beer. 6. Inventory candy (Inventory, int). Same as above but for candy. 7. Inventory chips (Inventory, int). Same as above but for chips Classes and function are new things to me. I don't completely understand them. Now here's my problem: Although I'm not exactly sure what the problem is, I know the problem lies in either the class definition, the function call, or the function definition. Everytime I run, the errors are: 'gas': cannot convert parameter 1 from 'int' to 'inventory'. ---- regarding the function call 'gas': local function definitions are illegal.----regarding the function definition. I know my question isn't specific, and I'm basically asking you to "make it work somehow". But if I know how to do just the first function, (gas function takes in an inventory and a quantity of gas. It then provides a price message to the customer and updates the Inventory. It returns the updated Inventory. ) I could probably do the rest. #include<iostream>
using namespace std;
class inventory
{
public:
int gas;
int pop;
int beer;
int candy;
int chips;
double pgas;
double ppop;
double pbeer;
double pcandy;
double pchips;
};
inventory gas(inventory, int);
int main()
{
cout<<"Starting Inventory\n"
<<"============================================\n"
<<" Gas 1000 Price: $3.00\n"
<<" Pop 1000 Price: $1.00\n"
<<" Beer 1000 Price: $2.50\n"
<<" Candy 1000 Price: $0.75\n"
<<" Chips 1000 Price: $1.00\n"
<<"============================================\n"
<<" Inventory Value: $8250.00\n\n";
inventory a;
a.gas=1000;
a.beer=1000;
a.pop=1000;
a.candy=1000;
a.chips=1000;
char choice;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"\nWhat would you like to buy?\n"
<<"Choices are: g(gas), s(sodapop), b(beer), c(candy), p(potato chips), q(quit):\n";
cin>>choice;
inventory b;
switch (choice)
{
case 'g':
int gallons;
cout<<"how many gallons?: ";
cin>>gallons;
cout<<gas(b.gas, gallons); //want to create function call
cout<<"Ok, "<<gallons<<" gallons. That'll be: $"<<endl;
break;
case 's':
cout<<"how many bottles?: ";
cin>>b.pop;
break;
case 'b':
cout<<"how many bottles?: ";
cin>>b.beer;
break;
case 'c':
cout<<"how many bars?: ";
cin>>b.candy;
break;
case 'p':
cout<<"how many bags?: ";
cin>>b.chips;
break;
case 'q':
cout<<"Present Inventory\n"
<<"============================================\n"
<<" Gas 1000 Price: $3.00\n"
<<" Pop 1000 Price: $1.00\n" //insert function calls
<<" Beer 1000 Price: $2.50\n"
<<" Candy 1000 Price: $0.75\n"
<<" Chips 1000 Price: $1.00\n"
<<"============================================\n"
<<" Inventory Value: $8250.00\n\n";
break;
default:
cout<<"That's not a valid choice, choose again.";
}
inventory gas(inventory, int) //sad attempt at function definition
{
inventory::gas=1000-int;
inventory::pgas=3*int;
return (pgas);
}
}Last edited by willj729; Oct 9th, 2005 at 11:56 AM. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Must.. tell... user... use... [code]... tags...
(please use [code] tags around your code when you post it so it doens't look like a bunch of hard to read crap) If you repost with [code] tags i'll read your code and help you out.
__________________
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 2
Rep Power: 0
![]() |
Sorry, but what do you mean by code tags? Comments in my code to guide you?
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
No, click the "Go Advanced" button if you are in the "Quick Reply" area.
Find the button on it that looks like "#". Paste your code into the box, select it, and the hit that button ("#"). Then post.
__________________
|
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The first thing you can't do is define a function inside another function. That's what this message is telling you:
Quote:
Fix that, and come back if you're still fuzzled.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|