1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib> //this is so str can be used
using namespace std;
struct Item //struct part of the program
{
string code;
string name;
double price;
};
const int MAX_ITEMS = 100; //limit the list to 100 items, can be increased
Item items[MAX_ITEMS];
int loadData(Item items[], int &count); //load part of the program
string getName(Item items[]); //get name and price part of the program
int count = 0; //setting count to 0
int main()
{
string reply;
cout << "Welcome to Grocery store. " << endl;
loadData(items, count); //load part of the program
getName(items); //get name and price part of the program
cout << "Thank you for shopping at Grocery." << endl;
cout << "Press q (or any other key) followed by 'Enter' to quit: ";
getline(cin, reply);
return 0;
}
int loadData(Item items[], int &count)
{
string inputFileName, input, reply;
ifstream inputFile;
//entering the name of the file with the list and prices
//cout << "Please enter the name of the backup file: ";
//getline(cin, inputFileName);
inputFileName = "prices.txt"; //direct location of file
inputFile.open(inputFileName.c_str());
if (!inputFile.is_open()) //if file isn't able to be opened correctly
{
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
exit(1);
}
while (getline(inputFile, input)) //upload the barcode, name and price
{
items[count].code = (input.substr(0,9).c_str()); //input of the barcode
items[count].name = (input.substr(10,26).c_str()); //input for the name
items[count].price = atof(input.substr(35,6).c_str()); //input for the price
count++;
}
cout << count << " items laded successfully" << endl;
return 0;
}
string getName(Item items[]) //search part of the program
{
string barcode;
double item, total;
item = 0; //setting item count to zero
total = 0; //setting total to zero
while(barcode != "q") //keep on running until q is entered
{
cout << "Please enter barcode (Press q to exit): ";
getline(cin, barcode);
int index = -1; //set index to -1
for(int i = 0; i < count; i++)
{
if(items[i].code.find(barcode) == 0)
{
index = i; //if barcode is found then change index to i
if(index == i) //if index is i then run
{
cout << setw(3) << right << items[i].name << setw(15) << left << items[i].price << endl;
item++;
total = items[i].price + total;
}
else
{
cout << "Item not found." << endl;
}
}
}
}
cout << "You bought "<< item << " items." << endl;
cout << "The total cost is " << total << endl;
}
|