Having problems reading from a file...

Hi,
I'm writing a program that at one point reads info from a file and is supposed to print it to the screen. To avoid the problem of having one variable read and print all of the info from the txt file I inserted an endl; every time I wrote info to the file. This works great until it reads the last line and starts to loop infinitely and I'm not sure why exactly. Maybe someone else can see something that I'm not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct item
{
    string id;
    string name;
    float itemCost;
    int markup;
};

void getInfo(item);
void printInfo(item);

const float CONVERT = 0.01;

    
int main(int argc, char *argv[])
{
    item info;
    info.id = "";
    info.name = "";
    info.itemCost = 0;
    info.markup = 0;
    char choice = ' ';
    
    do
    {
        cout << "welcome blah blah blah...." << endl;
        cout << "to exit item input simply enter * to finish" << endl;
        getInfo(info);
        cout << endl;
        printInfo(info);
        cout << "Run again?: ";
        cin >> choice;
    }while(choice == 'y' || choice == 'Y');
    cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

void getInfo(item info)
{
    ofstream outFile;
    string badInput;
    
    outFile.open("product.txt");
    
    cout << "Item number: ";
    cin >> info.id;
    while(info.id != "*")
    {
        outFile << info.id << endl;
        cout << "Item name: ";
        cin >> info.name;
        outFile << info.name << endl;
        cout << "Cost of item: ";
        while(!(cin >> info.itemCost))
        {
            cin.clear();
            cin >> badInput;
            cout << "Bad input... input must be a number." << endl;
            cout << "Cost of item: ";
        }
        outFile << info.itemCost << endl;
        cout << "Desired markup(must be a multiple of 5): ";
        do
        {
            while(!(cin >> info.markup))
            {
                cin.clear();
                cin >> badInput;
                cout << "Bad input... input must be a number and a multiple of 5." << endl;
                cout << "Desired markup (must be a multiple of 5): ";
            }
            if(info.markup > 50 || info.markup < 0 || info.markup % 5 != 0)
            {
                cout << "Bad input... input must be a number and a multiple of 5." << endl;
                cout << "Desired markup (must be a multiple of 5): ";
            }
        }while(info.markup > 50 || info.markup < 0 || info.markup % 5 != 0);
        
        outFile << info.markup << endl;
        cout << "Item number: " << endl;
        cin >> info.id;
    }
    outFile.close();
}
void printInfo(item info)
{
    ifstream inFile;
    
    inFile.open("product.txt");
    while(!inFile.eof())
    {
        getline(inFile, info.id);
        cout << info.id << endl;
        getline(inFile, info.name);
        cout << info.name << endl;
        inFile >> info.itemCost;
        cout << info.itemCost << endl;
        inFile >> info.markup;
        cout << info.markup << endl;
    }
    inFile.close();
}


The problem is at line 87 with the endl; when I remove it it doesn't loop and works fine but then it reads and prints out the markup and following id number into markup as one number.
Any thoughts/insight would be greatly appreciated!

Last edited on
Topic archived. No new replies allowed.