Making String arrays out of long files.

Ok so I have this code written and all it is supposed to do now is simply take the command line arguments, look in the file, and combine all the names in that file. Here is the code I have written.

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
void LoadArray( string student[], int argc, char * argv[],int& numOfStudents )
{
	for ( int n = 1; n <= argc; n++ )
	{
		ifstream inFile;
		inFile.open ( argv[n] );
		for ( ; inFile; numOfStudents++)
		{
			string first, middle, last;
			inFile >> first >> middle >> last;
			inFile.ignore( 100, '\n');
			if ( last == "Instructor" || last == "Student")
			{
				last = middle;
				middle = "flag";
			}
			if ( middle == "flag" )
			{
				student[numOfStudents] = first + " " + last;
			}
			else
			{
				student[numOfStudents] = first + " " + middle +  " " + last;
			}
		}
		inFile.close();
	}
}


But for some odd reason, it won't seem to do what it is supposed to do. Here is an example output:


Brian L Jackson
Caleb Xavier Harper
Cole Counts Woodward
David Anthony Ingle
Andrew J Worsey
Justin Harris
Liana Dorcelia Whorley
Matthew L Moon
Paul R Caraglio
Rachel Nicole Thomas

Corbin Tyler Osborne
Dr Zachariah Sinkala
Heather Marie Basile
Heidi A Chesak
James Paul Cooper
Jamie Caylin Hebl
Jordan L Qualls
Justin Harris
Luke Ivan Reves
Michael Curtis Robinson
Mr Jeffrey Eugene
Paul R Caraglio
Paul Samir Farag
Peter D Schwartz
Ryan Donald Wilson
Thierry Joseph Legrain
Tyler Brooke Loucky

Ashleigh N Eymard
Caelan Riley Reed
Gary D White
Dylan J Graves
Elizabeth Lauren Koppang
Herminia E Gonzalez
Jacquelyn Marie Zarcone
Jared Evan Staples
Jon P Michael
Jordan Lynn Watson
Joseph Michael Honea
Katelynne Marie-Jeanette Jones
Kyle Thomas Dugger
Lauren E Daignault
Lena Nichole Stacey
Michael Thomas Jamison
Nandi Chihombori-Quao
Omar David Chapa
Paul R Caraglio
Shawn Kristen Hooper
Sierra B Funte
Stephen Edward Porch
Troy Lee Coram
William Lanier Hutcheson



The issue with that output is it seems that at the end of every file it puts a blank line in the array.

PS:
Here is the code that I'm using to out put kinda simple but maybe the issue is with the output itself.
1
2
3
4
5
void OutputStudents( const string students[], int numOfStudents)
{
	for ( int i = 0; i < numOfStudents; i++)
		cout << students[i] << endl;
}
After line 10 you should check if inFile.fail(). If it failed, it means your variables, may very well don't have values in it. Only accept the input if inFile.fail() is false.
The issue is even after I include a if statement that looks like
1
2
3
4
5
6
7
string first, middle, last;
inFile >> first >> middle >> last;
if(!inFile.fail() )
{
     //all code from line 11-25
}
inFile.close();


It still gives me that blank line at the end of every file.
Last edited on
It seems to me that it's because your For loop simply tests that "InFile" is valid. "InFile" will bool to true until it tries to access an an invalid line, and since the variables "first", "middle" and "last" are all created and destroyed inside of the for loop they are blank when you write them to the file.

You realise that this will get all screwed up because you don't do anything for those people who have no middle name right?
Thanks for the advice that will most likely help. It makes sense at least haha. And well because of the ways the files look it does take for account them not having a middle name. The way the file looks like is somewhat like this.

first middle last instructor
first last student
first middle last student
etc..

That explains the if statement if ( last == "Instructor" || last == "Student" )
Topic archived. No new replies allowed.