Creating a code and compiling in g++ that works as an inventory program. Pretty simple stuff.
I'm getting a few errors/warnings that sound like simple errors but I need a fresh set of eyes to take a look. For both functions below I get this: "error: a function-definition is not allowed here before '{' token. I also get a warning and an error on the last line of the code. 1. warning: no newline at end of file. 2. error: expected '}' at the end of input.
To me it sounds like i'm just missing a curly brace somewhere, but everything looks like it lines up to me :x
void Inventory::printAllInfo(){
if(item_numbers==0){
cout<<"Error: No items in the inventory."<<endl;
}
else{
cout<<"\tYour inventory has information about the following "<<item_numbers+1<<" items:"<<endl;
for(int i=0;i<item_numbers;i++){
cout<<"\t Item information for "<<info[i].item_name<<endl;
cout<<"\t\tColor: "<<info[i].color<<endl;
cout<<"\t\tAmount: "<<info[i].amount<<endl;
cout<<"\t\tPrice: "<<info[i].price<<endl;
cout<<"\t\tLocation (Shelf Number): "<<info[i].location<<endl<<endl;
}
cout<<"\tYour inventory has a total of "<<units<<" items with a total price of "<<tprice<<endl;
}
}
int main(){
int select;
bool exit = false;
while(!exit){
cout<<"Select from the following menu:"<<endl;
cout<<"\t1. Add an item to the inventory."<<endl;
cout<<"\t2. Print the content of the inventory."<<endl;
cout<<"\t3. Search for an item in the inventory."<<endl;
cout<<"\t4. Delete an item from the inventory."<<endl;
cout<<"\t5. Exit."<<endl;
cout<<"Make your menu selection now: ";cin>>select;
switch(select){
case 1:
addItem();
break;
case 2:
searchforItem();
break;
case 3:
deleteItem();
break;
case 4:
printAllInfo();
break;
case 5:
exit=true;
break;
default:
cout<<"Not a valid selection."<<endl;
break;
}
}
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
constint MAX_ITEMS=100;
struct Item
{
string item_name; //item name
string color; //item color
int amount; //amount of items
double price; //item price
int location; //item location (shelf number)
};
class Inventory
{
public:
Inventory(); //default constructor: initializes the Inventory object to start with an empty inventory
void addItem(); //adds new item information to the inventory by updating the Info array with information about the item name
//color, amount, price and location
void searchforItem(); //asks the user to specify the name of an item and displays all the information from the inventory for this item if
//such an item exists, an error message otherwise
void deleteItem(); //deletes from the inventory the information for an item specified by the user.
//Updates the Inventory object accordingly
void printAllInfo(); //prints the information for the individual items in the inventory, and the total number of items and total price
private:
Item info[MAX_ITEMS]; //stores information about all items in the inventory
int item_numbers, units;
double tprice;
};
void Inventory::addItem(){
if(item_numbers==MAX_ITEMS){
cout<<"Error: Inventory is full."<<endl;
}
else{
cout<<endl<<"You are now adding a new item to your inventory."<<endl;
cout<<endl<<"\tEnter the name of the item: ";
cin>>info[item_numbers].item_name;
cout<<endl<<"\tEnter the color of this item: ";
cin>>info[item_numbers].color;
cout<<endl<<"\tEnter the number of items of this type: ";
cin>>info[item_numbers].amount;
cout<<endl<<"\tEnter the price of an item of this type: ";
cin>>info[item_numbers].price;
cout<<endl<<"\tEnter the location of this item(shelf number): ";
cin>>info[item_numbers].location;
item_numbers++;
units=units+info[item_numbers].amount;
tprice=tprice+(info[item_numbers].price*info[item_numbers].amount);
}
}
void Inventory::searchforItem(){
string itemsearch;
if(item_numbers==0){
cout<<"Error: No items in the inventory."<<endl;
}
else{
cout<<"Enter name of item in the inventory to search for: ";
cin>>itemsearch;
for(int i=0;i<item_numbers;i++){
if(info[i].item_name==itemsearch){
cout<<endl<<info[i].item_name;
cout<<endl<<info[i].color;
cout<<endl<<info[i].amount;
cout<<endl<<info[i].price;
cout<<endl<<info[i].location;
}
}
}
}
void Inventory::deleteItem(){
string delitem;
if(item_numbers==0){
cout<<"Error: No items in the inventory."<<endl;
}
else{
cout<<"Enter name of the item you want to delete from the inventory: ";
cin>>delitem;
for(int i=0;i<item_numbers;i++){
if(info[i].item_name==delitem){
units=units-info[i].amount;
tprice=tprice-info[i].amount*info[i].price;
for(int j=0;j<item_numbers-1;j++){
info[j].item_name=info[j+1].item_name;
info[j].color=info[j+1].color;
info[j].amount=info[j+1].amount;
info[j].price=info[j+1].price;
info[j].location=info[j+1].location;
}
item_numbers--;
cout<<"Info for item "<<delitem<<" has been deleted."<<endl;
}
}
}
}
void Inventory::printAllInfo(){
if(item_numbers==0){
cout<<"Error: No items in the inventory."<<endl;
}
else{
cout<<"\tYour inventory has information about the following "<<item_numbers+1<<" items:"<<endl;
for(int i=0;i<item_numbers;i++){
cout<<"\t Item information for "<<info[i].item_name<<endl;
cout<<"\t\tColor: "<<info[i].color<<endl;
cout<<"\t\tAmount: "<<info[i].amount<<endl;
cout<<"\t\tPrice: "<<info[i].price<<endl;
cout<<"\t\tLocation (Shelf Number): "<<info[i].location<<endl<<endl;
}
cout<<"\tYour inventory has a total of "<<units<<" items with a total price of "<<tprice<<endl;
}
}
int main(){
int select;
bool exit = false;
while(!exit){
cout<<"Select from the following menu:"<<endl;
cout<<"\t1. Add an item to the inventory."<<endl;
cout<<"\t2. Print the content of the inventory."<<endl;
cout<<"\t3. Search for an item in the inventory."<<endl;
cout<<"\t4. Delete an item from the inventory."<<endl;
cout<<"\t5. Exit."<<endl;
cout<<"Make your menu selection now: ";cin>>select;
switch(select){
case 1:
info[item_numbers].addItem();
break;
case 2:
info[item_numbers].searchforItem();
break;
case 3:
info[item_numbers].deleteItem();
break;
case 4:
info[item_numbers].printAllInfo();
break;
case 5:
exit=true;
break;
default:
cout<<"Not a valid selection."<<endl;
break;
}
}
return 0;
}
That was silly. I updated a few things and put the new code into my previous post. Only compilation errors I get now are that "info" and "item_numbers" aren't within the scope of main. I'm not sure where I should declare these though. I tried some different things, but can't quite get it right.
"info" and "item_numbers" are private fields in the Inventory class. So, what you need to do is to create an object of type Inventory and use the Inventory methods on it. You don't have to deal with the fields at all.