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
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
