Question on reading from File

Vidda

Senior member
Sep 29, 2004
614
0
0
My problem:

I need to read in candidates for an election from a file. There are 6 different races, and 2 candidates for each race. The file is formatted as such:

President
John Smith, Jane Smith
Vice President
John Smith, Jane Smith
etc. (names are different obviously)

I'm trying to store the names of the candidates into a [2][6] string array. I'm having problems making sure that only the two names of the candidates get stored into the array. Anyway, my idea was to do a simple input from a file and save to a string variable. I then wanted to compare the last character of the file. If it was a space, I would again read from the file and append the name to the end of the first input. I'm having errors with this, and I think it's due to the formatting of the file with the end line.

What I've tried: (this isn't exact, just a general idea)
ifstream candidates(file)
candidates >> x
if (x.at(x.length-1).compare(' ')
candidates >> y
x += y

I'm at a loss as to the correct syntax for comparing the last line of the string. Also, I've toyed around with the getline command, but I can't make that work. Any ideas?

Edit: C++ and using MSVS2k3
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
openfile
index = 0;
while (!EOF)
{
input a line from file
store the string as the position
input the next line from file
Parse out via the comma, the candidates
store the information into the indexed array
index++;
}
closefile
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Originally posted by: Common Courtesy
openfile
index = 0;

input a line from file

while (!EOF)
{

store the string as the position
input the next line from file
Parse out via the comma, the candidates
store the information into the indexed array
index++;

input a line from file
}
closefile

Shouldn't you do this so you don't get weird eof errors and such?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
I was trying to get him on track; not fill his mind with details.

In actual coding for myself

While (!EOF = Read Line)