Simple question

This is a discussion on Simple question within the C++ Programming forums, part of the General Programming Boards category; My program is a very simple program, but I am having a problem. It is designed to search a file ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    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;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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);

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    That did the trick. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21