• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question on reading from File

Vidda

Senior member
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
 
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
 
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?
 
I was trying to get him on track; not fill his mind with details.

In actual coding for myself

While (!EOF = Read Line)
 
Back
Top