I should be able to read the current totals and add additional inventory to text file for case 1.
C++ I'm having problems with this code. I need to read the text file and add the total inventory for case 2 and to list inventory total by make/model for case 3. It runs but doesnt provide results. I should be able to read the current totals and add additional inventory to text file for case 1.
book.txt
make
pearsons
model
galaxies
price
200.00
inventory
20
make
pearsons
model
islands
price
299.99
inventory
10
*************************************************************
#include
#include
using namespace std;
class Book
{
private:
string make, model;
float price;
int inventory;
public:
Book();
Book(string mk, string md, float p, int i);
void setMake(string mk)
{
make = mk;
}
string getMake()
{
return make;
}
void setModel(string md)
{
model = md;
}
string getModel()
{
return model;
}
void setPrice(float p)
{
price = p;
}
float getPrice()
{
return price;
}
void setInventory(int i)
{
inventory = i;
}
int getInventory()
{
return inventory;
}
};
Book::Book()
{
make = "Generic";
model = "Basic";
price = 500.00;
inventory= 0;
}
Book::Book(string mc, string md, float p, int i)
{
make = mc;
model = md;
price = p;
inventory = i;
}
int main()
{
int menu;
Book b[10];//declaring book array
int index=0;//to keep track total number of books
string make, model;
float price;
int inventory;
while(true){
cout << "\n Menu:\n";
cout << " 1. Add Inventory\n";
cout << " 2. Inventory total\n";
cout << " 3. Inventory by make/model \n";
cout << " 0. Exit\n";
cout << "\n > ";
cin >> menu;
switch (menu)
{
case 1:
//taking book information from user
cin.ignore();
cout<<"\nenter make : ";
getline(cin,make);
cout<<"\nenter model : ";
getline(cin,model);
cout<<"\nenter price : ";
cin>>price;
b[index]=Book(make,model,price,index+1);
index++;
cout<
break;
case 2:
//display all the content of book array
cout<<"\nTotal Inventory: "<
cout<
cout<
for(int i=0;i<=index-1;i++)
cout<
cout<
break;
case 3:
cin.ignore();
cout<<"\nenter make to search : ";
getline(cin,make);//taking make from user
for(int i=0;i<=index-1;i++){//looping over book array
if(b[i].getMake()==make){//if make found
cout<
cout<
cout<
}
}
cout<
break;
case 0:
exit(0);
default:
cout << "\n Select a valid number [Between 0 and 3]\n";
}
}
}
Step by step
Solved in 4 steps with 2 images