I've been working with this code for an assignment and the first time I tried to compile it my computer got stuck in a loop where the log would just jump from one file to the next in an attempt to compile. Closed, opened again, tried again, this time it threw all kinds of parse errors but I don't quite understand what the problem is.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
usingnamespace std;
int main()
{
int plu;
string productName;
int type;
double price;
double inventory;
c = 0;
ifstream original ("inventory.txt"); //read from original inventory file
if (original.is_open())
{
while (!original.eof())
{
original >> plu >> productName >> type >> price >> inventory;
Product temp = Product(plu, productName, type, price, inventory);
inv[c] = temp;
c++;
}
original.close();
}
int option;
do
{
cout << "WELCOME!" << endl;
option = menu();
if (option == 1)
checkout();
else
updateFile();
}
while(option != 0);
system ("pause");
return 0;
}
Product inv[100]; //array of 100 objects
int c = 100; //maximum products is 100
int menu(void) //display user options
{
int option;
cout << "Enter 1 to begin checkout or 0 to exit" << endl;
cin >> option;
return option;
};
void updateFile() //output results to a new file
{
ofstream newInvFile("newinventory.txt");
for (int p = 0; p < c; p++)
{
newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
}
}
void checkout(void)
{
double total = 0;
double quantity;
int input;
int PLU = 0;
do
{
PLU = 0;
cout << "Enter PLU code or (0) to exit" << endl;
cin >> input;
if (input == 0)
break;
for (int p = 0; p < c; p++)
{
if (inv[p].getPLU() == input)
{
PLU = p;
break;
}
}
if (inv[PLU].getType() == 1)// Determine whether product is sold by weight or units
{
cout << "Weight: ";
}
else
{
cout << "Quantity: ";
}
cin >> quantity;
total += quantity * inv[PLU].getPrice();
inv[PLU].updateInventory(quantity);
}
while (input != 0);
cout << "Total: $" << total << endl;
if (total > 50) //apply discount if total is over $50
{
total = total * 0.95;
cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << total << endl;
}
}
I got it to compile. You need to fix your client file first. Declare before you use. You need to use prototypes at top before main. Also you should have class declarations in the header and definitions in the .cpp.. and remove your constructors from being commented out.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
usingnamespace std;
// Some directions for the user would help to, no idea what plu equals what but I am not sure what
// your plans for this program or if you were handed a sheet of options.
int menu(void); // Prototype
void updateFile(); // Prototype
void checkout(void); //Prototype
Product inv[100]; //array of 100 objects ---> these need to be here
int c = 100; //maximum products is 100 ----> and not great idea to use global variables..
int main()
{
int plu;
string productName;
int type;
double price;
double inventory;
c = 0;
ifstream original("inventory.txt"); //read from original inventory file
if (original.is_open())
{
while (!original.eof())
{
original >> plu >> productName >> type >> price >> inventory;
Product temp = Product(plu, productName, type, price, inventory);
inv[c] = temp;
c++;
}
original.close();
}
int option;
do
{
cout << "WELCOME!" << endl;
option = menu();
if (option == 1)
checkout();
else
updateFile();
}
while (option != 0);
system("pause");
return 0;
}
int menu(void) //display user options
{
int option;
cout << "Enter 1 to begin checkout or 0 to exit" << endl;
cin >> option;
return option;
};
void updateFile() //output results to a new file
{
ofstream newInvFile("newinventory.txt");
for (int p = 0; p < c; p++)
{
newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
}
}
void checkout(void)
{
double total = 0;
double quantity;
int input;
int PLU = 0;
do
{
PLU = 0;
cout << "Enter PLU code or (0) to exit" << endl;
cin >> input;
if (input == 0)
break;
for (int p = 0; p < c; p++)
{
if (inv[p].getPLU() == input)
{
PLU = p;
break;
}
}
if (inv[PLU].getType() == 1)// Determine whether product is sold by weight or units
{
cout << "Weight: ";
}
else
{
cout << "Quantity: ";
}
cin >> quantity;
total += quantity * inv[PLU].getPrice();
inv[PLU].updateInventory(quantity);
} while (input != 0);
cout << "Total: $" << total << endl;
if (total > 50) //apply discount if total is over $50
{
total = total * 0.95;
cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << total << endl;
}
}
Thanks for you help! I really do appreciate it! And PLU is product label, its in the text file, supposed to be a sort of barcode number. Will clarify. Onto the random error I'm getting from the receipt program. Thanks again!