i have the following code that is suppose to read the input from a txt file and
ID: 3626357 • Letter: I
Question
i have the following code that is suppose to read the input from a txt file and loads the data into the member variablesthe problem i am having is when compiling i keep getting errors on line 158, 159 166 and 167 any ideas?
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class Product
{
public:
/**
A default constructor that sets all the private variables to
default values.
*/
Product();
/**
A three-parameter constructor that initializes
all three member variables to the values passed to the parameter.
*/
Product(string name, double price, int quantity);
/**
One "get" function for each private variable that returns the
current value. The name of the function must follow the naming
standards for "get" functions, which is the name of the variable
with the prefix word "get".
*/
string getName() const;
double getPrice() const;
int getQuantity() const;
/**
One "set" function for each private variable that sets a new value.
The name of the function must follow the naming standards for "set"
functions, which is the name of the variable with the prefix word "set".
Remember that you must define the "set" functions outside the class
declaration (curly braces of the class).
*/
void setName(string name);
void setPrice(double price);
void setQuantity(int quantity);
/**
Accessor function getValue() that has no parameters and returns
the price times the quantity.
*/
double getValue() const;
/**
Accessor function print() that has no parameters and returns no
values, but displays the data of the class all on one line like this:
*/
void print();
void read();
/**
Loads information about this Product from the file stream.
@param fin An input stream connected to the files with the
data to load.
*/
void read(ifstream& fin);
void write(ofstream& fout);
private:
string name;
double price;
int quantity;
};
void reportInventory (vector<Product>& store);
void addProduct(vector<Product>& store);
void deleteProduct(vector<Product>& store);
void sellProduct(vector<Product>& store);
void restockProduct(vector<Product>& store);
/**
loads the product data from specified file ame and returns data in a vector of product objects.
*/
void loadData(vector<Product>store,string fileName);
/**
writes store data to an output file.
*/
void saveData(vector<Product> store, string fileName);
int main()
{
Product product1;
Product product2("Crackers", 2.43, 12);
Product product3("BBQ Sauce", 2.95, 1);
product1.setName("Cereal");
product1.setPrice(3.95);
product1.setQuantity(5);
vector<Product> store;
loadData(store,"products.txt");
//store.push_back(product1);
//store.push_back(product2);
//store.push_back(product3);
int choice = 1;
while (choice != 0) {
cout << " Welcome To Rods corner bodega Please enter one of the following: "
<< "0. Exit Program "
<< "1. Reort Inventory "
<< "2. Add a new Product "
<< "3. Delete a product "
<< "4. Sell a product "
<< "5. Restock a product "
<< " Make a Choice(0-5): ";
cin >> choice;
cout << endl;
if (choice==0){
cout << " Thank you for Shopping";
return 0;
} else if (choice ==1){
reportInventory(store);
} else if (choice ==2) {
addProduct(store);
} else if (choice == 3) {
deleteProduct(store);
} else if (choice == 4) {
sellProduct(store);
} else if (choice == 5) {
restockProduct(store);
}else {
cout << " Invalid choice,choose a number between 1 and 5 ";
}
}
saveData(store,"store.txt");
return 0;
}
cout << "My Products : ";
cout << "Name" << setw(14) << "Price" << setw(6) << "Qty"
<< right << setw(9) << "Value ";
//product1.print();
//product2.print();
//product3.print();
return 0;
}
Product::Product()
{
name = "";
price = 0.0;
quantity = 0;
}
Product :: Product(string newName, double newPrice, int newQuantity)
{
name = newName;
price = newPrice;
quantity = newQuantity;
}
string Product :: getName() const
{
return name;
}
double Product :: getPrice() const
{
return price;
}
int Product :: getQuantity() const
{
return quantity;
}
void Product :: setName(string newName)
{
name = newName;
}
void Product :: setPrice(double newPrice)
{
price = newPrice;
}
void Product :: setQuantity(int newQuantity)
{
quantity = newQuantity;
}
double Product :: getValue() const
{
return price * quantity;
}
void Product :: print()
{
cout
<< left << setw(14) << name << right << setw(4) << fixed
<< setprecision(2) << price << right << setw(6) << quantity
<< right << setw(8) << price * quantity << endl;
}
void Product:: read(ifstream& fin) {
fin>> ws;
getline(fin,name);
fin>> price;
fin>> quantity;
}
/**
Loads the Product data from the specified file name and
returns the data in a vector of Product objects.
@param store The list of products to display.
@param fileName The name of the file from which to read.
*/
void loadData(vector<Product>& store,string filename) {
ifstream fin (filename.c_str());
if (fin fail())
{
cout<< "Input file failed to open. ";
exit(-1);
}
while(fin.good())
{
product temp;
temp.read(fin);
if(fin.good())
{
store.push_back(temp);
}
}
fin.close();
}
void Prouct:: write(ofstream& fout)
{
fout << name << endl;
fout << price <<endl;
fout << quantity<<endl;
}
/**
Writes store data to an output file.
@param store The vector of Product objects.
@param fileName The name of the file to which to write.
*/
void saveData (vector<Product> store,string filename)
{
ofstream fout(filename.c_str());
for( unsigned i= 0; i < stor.size(); i++)
{
store[i].write(fout);
}
void reportInventory (vector<Product>& store) {
cout << "# My products: "
<< setw(4) <<" Price"
<< setw(6) <<" Qty"
<< setw(8) <<"Value ";
for (unsigned i= 0; i < store.size(); i++) {
cout << i+ 1 << " ";
store[i].print();
}
}
void addProduct(vector<Product>&store) {
Product newProduct;
newProduct.read();
store.push_back(newProduct);
}
void deleteProduct(vector<Product>& store) {
reportInventory(store);
int num;
cout << "Enter the number of product you wish to delete.";
cin >> num;
for (unsigned i = num - 1; i< store.size() - 1; i++) {
store[i] = store [i+1];
}store.pop_back();
}
void sellProduct(vector<Product>& store) {
reportInventory(store);
int numProd,prodQt;
cout << "Please enter the number of the product you wish to sell.";
cin >> numProd;
cout << " Please enter quantity of the product you want.";
cin >> prodQt;
store.at(numProd-1).getPrice() * prodQt;
store.at(numProd-1).setQuantity(store.at(numProd-1).getQuantity() - prodQt);
cout<<endl;
}
void restockProduct(vector<Product>& store) {
reportInventory(store);
int numProd, prodQt;
cout << "Please enter the number of the product you wish to restock.";
cin >> numProd;
cout << " Please enter the restock quantity.";
cin >> prodQt;
store.at(numProd-1).setQuantity(store.at(numProd-1).getQuantity() + prodQt);
cout<< store.at(numProd-1).getName()<<endl;
}
Explanation / Answer
please rate - thanks
you had an extra }
I then got rid if other errors to get it to compile
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class Product
{
public:
/**
A default constructor that sets all the private variables to
default values.
*/
Product();
/**
A three-parameter constructor that initializes
all three member variables to the values passed to the parameter.
*/
Product(string name, double price, int quantity);
/**
One "get" function for each private variable that returns the
current value. The name of the function must follow the naming
standards for "get" functions, which is the name of the variable
with the prefix word "get".
*/
string getName() const;
double getPrice() const;
int getQuantity() const;
/**
One "set" function for each private variable that sets a new value.
The name of the function must follow the naming standards for "set"
functions, which is the name of the variable with the prefix word "set".
Remember that you must define the "set" functions outside the class
declaration (curly braces of the class).
*/
void setName(string name);
void setPrice(double price);
void setQuantity(int quantity);
/**
Accessor function getValue() that has no parameters and returns
the price times the quantity.
*/
double getValue() const;
/**
Accessor function print() that has no parameters and returns no
values, but displays the data of the class all on one line like this:
*/
void print();
void read();
/**
Loads information about this Product from the file stream.
@param fin An input stream connected to the files with the
data to load.
*/
void read(ifstream& fin);
void write(ofstream& fout);
private:
string name;
double price;
int quantity;
};
void reportInventory (vector<Product>& store);
void addProduct(vector<Product>& store);
void deleteProduct(vector<Product>& store);
void sellProduct(vector<Product>& store);
void restockProduct(vector<Product>& store);
/**
loads the product data from specified file ame and returns data in a vector of product objects.
*/
void loadData(vector<Product>store,string fileName);
/**
writes store data to an output file.
*/
void saveData(vector<Product> store, string fileName);
int main()
{
Product product1;
Product product2("Crackers", 2.43, 12);
Product product3("BBQ Sauce", 2.95, 1);
product1.setName("Cereal");
product1.setPrice(3.95);
product1.setQuantity(5);
vector<Product> store;
loadData(store,"products.txt");
//store.push_back(product1);
//store.push_back(product2);
//store.push_back(product3);
int choice = 1;
while (choice != 0) {
cout << " Welcome To Rods corner bodega Please enter one of the following: "
<< "0. Exit Program "
<< "1. Reort Inventory "
<< "2. Add a new Product "
<< "3. Delete a product "
<< "4. Sell a product "
<< "5. Restock a product "
<< " Make a Choice(0-5): ";
cin >> choice;
cout << endl;
if (choice==0){
cout << " Thank you for Shopping";
return 0;
}
else if (choice ==1){
reportInventory(store);
}
else if (choice ==2) {
addProduct(store);
}
else if (choice == 3) {
deleteProduct(store);
}
else if (choice == 4) {
sellProduct(store);
}
else if (choice == 5) {
restockProduct(store);
}
else {
cout << " Invalid choice,choose a number between 1 and 5 ";
}
}
saveData(store,"store.txt");
//return 0;
//}
cout << "My Products : ";
cout << "Name" << setw(14) << "Price" << setw(6) << "Qty"
<< right << setw(9) << "Value ";
//product1.print();
//product2.print();
//product3.print();
return 0;
}
Product::Product()
{
name = "";
price = 0.0;
quantity = 0;
}
Product :: Product(string newName, double newPrice, int newQuantity)
{
name = newName;
price = newPrice;
quantity = newQuantity;
}
string Product :: getName() const
{
return name;
}
double Product :: getPrice() const
{
return price;
}
int Product :: getQuantity() const
{
return quantity;
}
void Product :: setName(string newName)
{
name = newName;
}
void Product :: setPrice(double newPrice)
{
price = newPrice;
}
void Product :: setQuantity(int newQuantity)
{
quantity = newQuantity;
}
double Product :: getValue() const
{
return price * quantity;
}
void Product :: print()
{
cout
<< left << setw(14) << name << right << setw(4) << fixed
<< setprecision(2) << price << right << setw(6) << quantity
<< right << setw(8) << price * quantity << endl;
}
void Product:: read(ifstream& fin) {
fin>> ws;
getline(fin,name);
fin>> price;
fin>> quantity;
}
/**
Loads the Product data from the specified file name and
returns the data in a vector of Product objects.
@param store The list of products to display.
@param fileName The name of the file from which to read.
*/
void loadData(vector<Product> store,string filename) {
ifstream fin (filename.c_str());
if (fin.fail())
{
cout<< "Input file failed to open. ";
exit(-1);
}
while(fin.good())
{
Product temp;
temp.read(fin);
if(fin.good())
{
store.push_back(temp);
}
}
fin.close();
}
void Product:: write(ofstream& fout)
{
fout << name << endl;
fout << price <<endl;
fout << quantity<<endl;
}
/**
Writes store data to an output file.
@param store The vector of Product objects.
@param fileName The name of the file to which to write.
*/
void saveData (vector<Product> store,string filename)
{
ofstream fout(filename.c_str());
for( unsigned i= 0; i < store.size(); i++)
{
store[i].write(fout);
}
}
void reportInventory (vector<Product>& store) {
cout << "# My products: "
<< setw(4) <<" Price"
<< setw(6) <<" Qty"
<< setw(8) <<"Value ";
for (unsigned i= 0; i < store.size(); i++) {
cout << i+ 1 << " ";
store[i].print();
}
}
void addProduct(vector<Product>&store) {
Product newProduct;
string filename;
cout<<"enter filename: ";
cin>>filename;
ifstream fin (filename.c_str());
if (fin.fail())
{
cout<< "Input file failed to open. ";
exit(-1);
}
newProduct.read(fin);
store.push_back(newProduct);
}
void deleteProduct(vector<Product>& store) {
reportInventory(store);
int num;
cout << "Enter the number of product you wish to delete.";
cin >> num;
for (unsigned i = num - 1; i< store.size() - 1; i++) {
store[i] = store [i+1];
}store.pop_back();
}
void sellProduct(vector<Product>& store) {
reportInventory(store);
int numProd,prodQt;
cout << "Please enter the number of the product you wish to sell.";
cin >> numProd;
cout << " Please enter quantity of the product you want.";
cin >> prodQt;
store.at(numProd-1).getPrice() * prodQt;
store.at(numProd-1).setQuantity(store.at(numProd-1).getQuantity() - prodQt);
cout<<endl;
}
void restockProduct(vector<Product>& store) {
reportInventory(store);
int numProd, prodQt;
cout << "Please enter the number of the product you wish to restock.";
cin >> numProd;
cout << " Please enter the restock quantity.";
cin >> prodQt;
store.at(numProd-1).setQuantity(store.at(numProd-1).getQuantity() + prodQt);
cout<< store.at(numProd-1).getName()<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.