Stuck in an infinite loop

Here's an example of the file I'm working with:

Saw 10 33.82
Drill 8 27.58
Hammer 23 76.34
Nail 13 9.54
Screwdriver 54 10.34
Boxcutter 23.11

I'm trying to read the data from a text file, one line at a time and put it into itemLine and then call BuildOneAttribute where I put the name into a structure I made called "Hardware" and then call BuildOneAttribute two more times to put the quantity (2nd number) and pirce (third number) also into the structure.

My while loop isn't ending though and I'm not sure what the problem is, it also doesn't seem to be reading the second line and just stopping after the first.

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
void ReadData (MyString filename)
{
    int count = 0;
	char itemLine[MAX_LINE];
	int size = 0;
	ifstream inFile;
    char temp[MAX_STRING];

	inFile.open(filename);


		size = MAX_LINE;


	while ( !inFile.getline( itemLine, MAX_LINE, '\n' ).eof() )
    {
        cout << itemLine;
        
        BuildOneAttribute(itemLine, temp, count);


        //count = count + 1;
    };


}

void BuildOneAttribute (char itemLine[], char temp[], int count)
{
    int i = 0;

        if (itemLine[i] =! 0)
    {
        temp[i] = itemLine[i];
        i = i + 1;
    }

        return;

}
Just a note: you don't need '\n' since default delimiter is '\n'.

Get rid of semicolon in }; on line 23.

=! should be !=.


It seems like you're trying to loop something inside BuildOneAttribute, to do that you use while-loops or for-loops, but instead you have an if statement.

Here is a simple working fstream program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream ifs("t.txt");

	const unsigned int MAX_DIGIT = 10;

	char storage[MAX_DIGIT];

	while(!ifs.getline(storage, MAX_DIGIT).eof())
	{
		cout << storage << endl;

	}

	ifs.close();
	return 0;
}

Last edited on
Topic archived. No new replies allowed.