I'm sure that this will be a simple matter for the majority of you but I hope you'll bear with my simple inquiry and if I'm posting in the wrong place I would certainly like to know that as well.
Here Goes:
I have a list in a text file that is formatted like so:

firstName lastName
firstName lastName
Position Position
Position can be one or two words.
I'm tryin to read the file into 3 arrays one for the first and last names and one for the positions.

But I never get a good result" if position has more than 1 word.
Here's what I'm trying so far:

(As you can see by the remmed statements I've been playing with peek() and ignore() trying to find a solution. )

I sure would appreciate an assist on this. TIA

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

using namespace std;

int main()
                char peeking[9];
	string first[9];
	char sentry;
	string last[9];
	string position[9];
	fstream inFile;
	for(int test = 0; test < 9; test++)
		inFile >> first[test];
		inFile >> last[test];

		//peeking[test] = inFile.peek();

	for(int test = 0; test < 9; test++)
		cout << "this is first " << test << " - " << player[test] << endl;
		cout << "this is last" << test << " - " << garbage[test] << endl;
		cout << "this is position " << test << " - " << position[test] << endl;

	return 0;

Recommended Answers

All 2 Replies


You'll find there's a C++ forum in the Software Development section, which is a better place for this question.

You were on the right track with the ignore( ) function, but you used it improperly.
Your goal with that is to remove any trailing spaces and the newline after reading in the last name. You could do that using getline( ) there, as you did for the position, after removing the blank(s) between the names. Or, use ignore( ) in this way:


You need to give a first parameter of 1 or greater, and since we're looking for a newline, a few extra in this parameter does no harm and will help if there are unseen trailing blanks. The character for newline is "escape n" which is written using the backslash, not the forward slash.

Thank you very much for your assistance :)
And also for the direction to the proper forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.