I am looking to make a small program that will cut down the repetetive need to insert BB tags in to achievements lists on a site I work for.

So far I have got the algorithm sucessfully reading the first line of the file, and outputting it using cout. The part im having difficulty with is reading all the lines underneath while combining them with the BB tags.

So far I have

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	string	name;
	int points;
	string description;
	ifstream inFile;
	

	inFile.open("C:\\Users\\Andy\\Desktop\\codegen.txt");

	if (!inFile) {
		cout << "Unable to open file";
		exit(1);
	}

	inFile >> name >> points >> description;

        // This is where I get stuck, I know I need a while statement with an EoF argument
        // Just most examples use getline, which is not what I want.
	while(! inFile.eof()){
		cout << "[b]" << name << " - [i]" << points << "[/i][/b]" << endl;
		cout << description << endl;
		inFile >> name >> points >> description;
	}

	inFile.close();

	return 0;

}

The txt file im reading from will have the following format

Name of achievement     points of achievement      decription of achievement
Name of achievement     points of achievement      decription of achievement
Name of achievement     points of achievement      decription of achievement

The name and the description are strings and need to allow blanks, now is it the txt file im going wrong with or i'm just forgetting a bit of code i was taught :/

Thanks

line 26: eof() doesn't work like that. Use this instead

while( inFile >> name >> points >> description )
{
   // blabla
}

But that assumes each line contains exactly three words with no embedded spaces. For example it won't read a name such as "John Smith" correctly.

Ok,

Still none the wiser though of how to get it to drop to the next line?

An example of the data it will be storing is this:

No rest for the wicked 5 All Stars & GS token in one run.

It's an imperfect world we live in. If the name of achievement field can be of variable length and have variable amount of whitespace and you can't restrcture the file by including a delimiting char of some sort, then one approach I can think of is to view each line as a string and eadh ward in the line as a substring or token. With each new line read in the fiest word and assign it to the name of achiement string. Then, read in the next word. If the first car of the current word is numerical as opposed to alphabetical, then it is the points of acievement field. Else it is still a word that belongs in the name of achievement string so concatenate with that string. Repeat until the points of acievement field has been found. Once the points of acievement field has been read in, it can be changed to a numerical value if desired. Once the points of achievement has been read in the rest of the line can be read into the description of achievement using getline(), as long as you clear the inut buffer first. An alternative would be to read the entire line into a stringstream and use the stringstream to parse the string with the same logic as above.

I can restructure the File if need be, all i require that it allows me to enter those 3 fields of data multiple times

If you can control how the file is written then I'd write the following information

No rest for the wicked
5
All Stars & GS

Bewitched
2
Mudbloods

to file as follows,

No rest for the wicked# 5 All Stars & GS

Bewitched# 2 Mudbloods


if all the information has to be on one line. Note how the # delimits the name of achievement field from the points of achievement. Now the file could be read using getline() to read the name of achievement file, >> to read the points of achievement, and getline() to read the description of achievement. Again, you need to take care if you use >> and getline() in the same program, so you may want to use some other combination of input methods, but at least the # delimiter (by the way, the # could have been any char unlikely to show up in the name of acheivement field) allows for much easier reading of the file.

Ok yeah I have full control over how the file is laid out, so have gone with your suggestion of

Name
Points
deacription

Now how would i get it to go down the lines, and more importantly how to get on to the next one? I.E

Name
Points
deacription
Name
Points
deacription
Name
Points
deacription
Name
Points
deacription

If it's one field per line and 3 fields per object then you can use three separate calls to getline().

while(first call to getline)
{
second call to getline
third call to getline
}

first call to getline will get name, second will get points and third will get description. You can use getline with the default newline delimiter and since you're not mixing >> with getline() you don't have to worry about what's left behind in the input stack after >> vs after getline(), etc. It's the best of all worlds, at least in terms of parsing.

This question has already been answered. Start a new discussion instead.