-
Simple question
My program is a very simple program, but I am having a problem. It is designed to search a file for the user's input then do ect. My problem is that it works fine if you enter an input that can be found in the file, untill you enter one that isn't. I ask for the user to enter another input, but no matter what input they enter it says its invalid now. ex. 15, 155, 1100 all work i then enter 155555 just to test it. It displays everything I want then I enter 15 again and it doesnt work. I'm guessing the area in bold is the problem area.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
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. Create a Reciept" << endl;
}
int main()
{
ifstream fin;
ofstream fout;
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;
menu = 1;
fin.open("grocery_item.txt", ios::in);
if (!fin.good())
{
cout << "Error loading grocery_item.txt";
return 1;
}
fout.open("Harris_Cody.txt", ios::out);
if (!fout.good())
{
cout << "Error loading Harris_Cody.txt";
return 1;
}
fout << setw (15) << "Item Number" << setw (20) << "Item Name" << setw (10) << "Unit" << setw (10) << " Unit price" << setw (10) << "Tax" << setw (10) << "Sub-total" << endl;
fout << setw (15) << "-----------" << setw (20) << "----------" << setw (10) << "----------" << setw (10) << "----------" << setw (10) << "----------" << setw (10) << "----------" << endl;
for(;;)
{
display_menu();
cin >> menu;
if (menu == 1)
{
cout << "What is the item number? ";
cin >> Inum1;
while (getline(fin, line))
{
istringstream instream;
instream.clear();
instream.str(line);
if (instream >> Inum >> name >> letter1 >> letter2 >> price)
{
if (Inum == Inum1)
{
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;
fout << setw (15) << Inum << setw (20) << name << setw (10) << more1 << setw (10) << price << setw (10) << total1 << setw (10) << total2 << endl;
}
else if (letter1 == 'n')
{
total1 = 0;
total2 = total;
total3 += total2;
cout << total3 << " Is your sub-total" << endl;
fout << setw (15) << Inum << setw (20) << name << setw (10) << more1 << setw (10) << price << setw (10) << total1 << setw (10) << total2 << 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;
fout << setw (15) << Inum << setw (20) << name << setw (10) << more << setw (10) << price << setw (10) << total1 << setw( 10) << total2 << endl;
}
if (letter1 == 'n')
{
total1 = 0;
total2 = total;
total3 += total2;
cout << total3 << " Is your sub-total" << endl;
fout << setw (15) << Inum << setw (20) << name << setw (10) << more << setw (10) << price << setw (10) << total1 << setw (10) << total2 << endl;
}
}
else if (letter2 == 's')
{
more = 1;
total1 = 0;
total2 = price;
total3 += price;
cout << total3 << " Is your sub-total" << endl;
fout << setw (15) << Inum << setw (20) << name << setw (10) << more << setw (10) << price << setw (10) << total1 << setw (10) << total2 << endl;
}
break;
}
}
}
if (fin.eof())
cout << "Invalid input, try again." << endl << endl;
fin.seekg(0);
}
else if (menu == 2)
{
fout << setw (65) << "TOTAL" << setw (10) << total3;
break;
}
}
fout.close();
fin.close();
cout << "Have a nice day!!" << endl;
system("PAUSE");
return 0;
}
-
Code:
if (fin.eof())
cout << "Invalid input, try again." << endl << endl;
fin.seekg(0);
that's not enough. EOF is a stream errorstate. You have to clear that error
e.g.
Code:
if (fin.eof()){
cout << "Invalid input, try again." << endl << endl;
fin.clear();
}
fin.seekg(0);
-
That did the trick. Thanks for the help.