I'm making a program that asks for an item number and if it matches an item number in the text file it will then determine its next move by what variable is displayed on the line that the item number is on. So, lets say I have a text file:
"Item number, Item name, taxability, unit type, price" and its 100 lines long. I need to match the user input to the item number then use the taxability variable, unit type, and price to get a sub-total. Then loop it to keep asking for an item number, ect. I'm a beginner, but from my code it looks like it "should" work.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void display_menu()
{
cout << "Choose from the menu"<< endl << endl;
cout << "Menu" << endl;
cout << "1. Add new item to purchases" << endl;
cout << "2. View Reciept" << endl;
}
int main()
{
ifstream fin;
string name, line;
char letter1, letter2;
double total1, total, total2, total3, more1, num;
float price;
int menu, Inum1, Inum, more;
num = .05;
total3 = 0;
total = 0;
total1 = 0;
total2 = 0;
fin.open("grocery_item.txt", ios::in);
if (!fin.good())
{
cout << "Error loading grocery_item.txt";
return 1;
}
while (getline(fin, line))
{
istringstream instream;
instream.clear();
instream.str(line);
if (instream >> Inum >> name >> letter1 >> letter2 >> price)
{
instream >> ws;
display_menu();
cin >> menu;
if (menu == 1)
{
cout << "What is the item number";
cin >> Inum1;
if (Inum1 == Inum)
{
if (letter2 == 'n')
{
cout << "How many units?" << endl;
cin >> more1;
total = more1 * price;
if (letter1 == 't')
{
total1 = total * num;
total2 = total1 + total;
total3 += total2;
cout << total3 << " Is your sub-total" << endl;
}
else if (letter1 == 'n')
{
total2 = total;
total3 += total2;
cout << total3 << " Is your sub-total" << endl;
}
}
else if (letter2 == 'p')
{
cout << "How many pounds?" << endl;
cin >> more;
total = more * price;
if (letter1 == 't')
{
total1 = total * num;
total2 = total1 + total;
total3 += total2;
cout << total3 << " Is your sub-total" << endl;
}
if (letter1 == 'n')
{
total2 = total;
total3 += total2;
cout << total3 << " Is your sub-total" << endl;
}
}
else if (letter2 == 's')
{
total3 += price;
cout << total3 << " Is your sub-total" << endl;
}
}
}
}
}
fin.close();
cout << "Have a nice day!!" << endl;
system("PAUSE");
return 0;
}