Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,099 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,638 people online right now. Registration is fast and FREE... Join Now!




Reading from a file

 
Reply to this topicStart new topic

Reading from a file

ch68813
2 Oct, 2007 - 07:12 PM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2007
Posts: 1


My Contributions
I'm trying to make a program that reads input from a .txt file that is in the format of:
Item number, Name, Taxability, unit count, price
127 butter_1 t s 1.99
183 candy4 t s 3.99
184 candy3 t s 1.45
153 detergent2 t s 4.87

I'm having trouble getting my program to actually search for the other variables in the line when the item number is entered. My program only works for the firsts line of my text file.

CODE
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void display_menu()
{
    cout << "Choose from the menu"<< endl << endl;
    cout << "Menu" << endl;
    cout << "0. Quit the program" << endl;
    cout << "1. Find purchase total/ Add new item" << endl;
    cout << "2. View Reciept" << endl;
}
int main()
{
    ifstream fin;
    
    string name;
    char letter1, letter2;
    double num;
    double total1, total, total2, total3;
    double more;
    float price;
    int menu, more1, Inum1, Inum;
    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;
    }
    do
    {
        display_menu();
        cin >> menu;
        if (menu == 1)
        {
            while (fin >> Inum >> name >> letter1 >> letter2 >> price)
            {
                    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;
                        }
                    }
            }
        }
        }while (menu != 0);
    fin.close();
    cout << "Have a nice day!!" << endl;
        system("PAUSE");
        return 0;
}

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Reading From A File
3 Oct, 2007 - 12:26 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
your while statement is probably causing the problem. i've snipped out some huge sections of code and moved the looping around a little bit, resulting in the following for the main routine:

CODE

int main() {
    ifstream fin;
    
    string name;
    char letter1, letter2;
    double num;
    double total1, total, total2, total3;
    double more;
    float price;
    int menu, more1, Inum1, Inum;
    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;
    }
    do {
        display_menu();
        cin >> menu;
        if (menu == 1) {
            fin.seekg(0); //resets 'get' pointer to start of file
            cout << "What is the item number";
            cin >> Inum1;
            
            do {
                fin >> Inum >> name >> letter1 >> letter2 >> price;
            } while (Inum != Inum1);
            cout << "Item chosen is: "<< name << endl;

            //    DO YOUR PRICING STUFF HERE
            
        }
    } while (menu != 0);
    fin.close();
    cout << "Have a nice day!!"<< endl;
    system("PAUSE");
    return 0;
}


after the user selects menu option 1, the file pointer is set to the beginning of the file - very important if the last item selected is after the one the user is going to select. after getting the item number, the do-while loop reads in a line of data from the file, until the appropriate item number is found. then you would do your pricing stuff.

for future projects, you may want to take the incremental approach. the critical aspect here is file i/o and searching, so get that part of the algorithm set before you add in any of the operations that you are going to perform on the inputted data.

hope that helps.

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:44PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month