I need help reading this file

I posted this in General C++ Programming but I think it's more appropriate here.


I have to read all of it.
I can read first line with getline

3-5 lines
are divided like this
amount | shape/size | name

string recipe;
int serving;
int amount;
string shape_size;
string name;

S'mores
2
4 squares graham crackers
1 bar milk chocolate
2 large marshmallows

I have tried this;

std::getline(f,recipe)
f >> serving;
f >> amount >> shape_size;
std::getline(f,name)

it's not taking the correct values, actually it's not even reading anything based on my output test.
Last edited on
add a delimiter.

getline has a default delimiter of '\n'( newline, carriage return( enter ) ).

getline( f, recipe, ' ' ); // reads until a space is found
but getline wouldn't get integers
here's my code so far

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
vector<string> food;
vector<int> serVings;
vector<int> pieces;
vector<string> amOunt;
vector <string> ingredient;

inFile.open ("recipe0.dat");
    if(!inFile)
    cout << "No file found";

while(!inFile.eof())
{
    string Nam;
    int servings;
    int piece;
    string AmT;
    string ingred;

        getline(inFile, Nam);
        inFile >> servings;
        inFile >> piece;
        getline(inFile, AmT, ' ');
        getline(inFile, ingred);

        food.push_back(Nam);
        serVings.push_back(servings);
        pieces.push_back(piece);
        amOunt.push_back(AmT);
        ingredient.push_back(ingred);


}


inFile.close();
for (int i = 0; food.size() > i; i++)
    cout << food[i] << endl;


it outputs nothing.
Output should just be

S'mores
Line 22 is not actually collecting any data. I don't have time now, but when I get back, I'll have a look again! (:
Ok. So I had a go at fixing your program. First off, you were only outputting the food vector, that's why there was no other data in the console!

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
#include <vector>
#include <sstream> // sstream also include <string>, so no need to add it.
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	vector<string> food;
	vector<int> serVings;
	vector<int> pieces;
	vector<string> amOunt;
	vector <string> ingredient;

	ifstream inFile;
	inFile.open ("recipe0.dat");
		if(!inFile)
		cout << "No file found";

	while(!inFile.eof())
	{
		string Nam;
		int servings;

		string tempPiece;
		int piece;

		string AmT;
		string ingred;

			getline(inFile, Nam);
			inFile >> servings;
			getline( inFile, tempPiece, ' ' );
			getline(inFile, AmT, ' ');
			getline(inFile, ingred);

			// Create a stringstream object and pass it the string.
			stringstream ss( tempPiece );

			// If the conversion to int fails, break from the loop.
			if( ! ( ss >> piece ) )
				break;

			// Clear the ss object.
			ss.clear();

			food.push_back(Nam);
			serVings.push_back(servings);
			pieces.push_back(piece);
			amOunt.push_back(AmT);
			ingredient.push_back(ingred);
	}


	inFile.close();

	//create some iterators for the vectors.
	vector< string >::iterator fIt = food.begin();
	vector< int >::iterator sIt = serVings.begin();
	vector< int >::iterator pIt = pieces.begin();
	vector< string >::iterator aIt = amOunt.begin();
	vector< string >::iterator iIt = ingredient.begin();

	// Increament each vector iterator. This is a bad way though, read below.
	for ( fIt; fIt != food.end(); ++fIt, ++sIt, ++pIt, ++aIt, ++iIt )
	{
		cout << *fIt << '\n';
		cout << *sIt << '\n';
		cout << *pIt << ' ' << *aIt << ' ' << *iIt << '\n';
	}

	return 0;
}


So, this program will read and store:
S'mores
2
4 squares graham crackers


The loop at the end it bad, because when you read in more ingredients, the next two lines;
1 bar milk chocolate
2 large marshmallows


You will have to some how keep a track of how many 'lines' of ingredients there are. You will also have to make a loop for each of the vectors, because sizes will vary and outputting them in the same loop will cause a violation error( Reading out of bounds of the vector ).
Topic archived. No new replies allowed.