Search file for user input

This is a discussion on Search file for user input within the C++ Programming forums, part of the General Programming Boards category; I'm making a program that asks for an item number and if it matches an item number in the text ...

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

    Search file for user input

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

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    	if (!fin.good())
    	{
    		cout << "Error loading grocery_item.txt";
    		return 1;
    	}
    Might want to print a newline there. Or an endl. You might also want to assign "grocery_item.txt" to a variable and use that rather than typing out "grocery_item.txt" over and over again. (Okay, twice.)

    Just from the title I'm guessing you want the user to enter an item number, and then search the entire file for that item number, etc, is that right? Because that's not what the code does right now.

    Have you tested your code?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Thats exactly what I want it to do. I tested it and it only works once for the first item number in the file. I'm very new to this and what I have been bonking my head over is how to make my program search my text file for the user's input.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're going to have to search the whole file for each item number. This means that you have to read every line in the file, comparing it with the item number until you find a match or you reach the end of the file; and then rewind the file to the beginning so that you can start searching for the next number. Or, alternatively, you could read the whole file into memory into an array of lines, say, and search through that.

    Assuming that you want to use the first method . . . I think file.seekg(0) will rewind the file once you're done with it. The code might look something like this:
    Code:
    repeat until done
        get an item number from the user
        for every line in the file
            parse the line into item numbers, etc
            if the item number entered by the user is equal to the one in the file
                do whatever you do for a successful match
                end the for-every-line-in-the-file loop
        if the end of the file was reached
          do whatever you do when there is no matching item number in the file
        rewind the file to the beginning
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    I think my problem lies in this part
    Code:
    repeat until done
        get an item number from the user
        for every line in the file
            parse the line into item numbers, etc
    I'm not to knowledgable in what kind of loops I should be using in these areas or how to parse my variables.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're already "parsing your variables" with this code:
    Code:
    if (instream >> Inum >> name >> letter1 >> letter2 >> price)
    What types of loops should you use? It doesn't really matter. Personally, I might go with something like this:
    Code:
    for(;;) {
        cout << "What is the item number? ";
        if(cin >> Inum1 == 0) break;
    
        while (getline(fin, line)) {
            istringstream instream;
    	instream.clear();
    	instream.str(line);
    	if (instream >> Inum >> name >> letter1 >> letter2 >> price) {
                if(Inum == Inum1) {
                    /* found a match */
                    break;
                }
            }
        }
    
        fin.seekg(0);
    }
    Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    I got it now... thank you SOOOOOO much!!!
    Last edited by ch68813; 10-03-2007 at 03:26 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That's strange. It looks like it should work, in theory. Unfortunately I'm out of time; perhaps someone else can help you.

    The code
    Code:
    fin.seekg(0);
    should probably be within
    Code:
    if (menu == 1)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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