When I attempt to output the array in the second function using ofstream I get nothing. I feel the character getline is not written correctly buy I'm unable to find the correct way.
2) Well... I was going to talk about data structures, defining your own custom types and using C++ containers... instead of 2-dimensional char array LastName and separate long array NumVotes. They are closely connected to each other, and shouldn't be separate.
I'm attempting to help my girlfriend with her homework so according to the assignment were not allowed to use structs.
Terrible. A well designed exercise should help you learn to use features of a language, not force you to do things the hard way.
But how about using std::string? An array of them. C++ strings are modern equivalents of old C char arrays. They resize automatically as needed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
usingnamespace std; // this is a bad habit, at some point it must be unlearned
// ...
string s; // empty string
getline(cin, s); // s is resized as needed
// ...
string LastName[NumCandidates]; // simpler, no?
Part of the problem is that you're mixing formatted input extraction with unformatted extraction (getline/get and operator>>) and your idea of how to deal with that is to sprinkle cin.get() throughout the code and hope it solves your problem. It doesn't, however. It makes things worse.
For instance, on line 22 you extract via operator>> an int value. This extraction will leave (at least) a newline that will trip up the unformatted extraction on line 39. Line 39 will see the newline left in the input stream and extract an empty string. Unfortunately, you have cin.get() on the same line which serves to hide from you where the actual problem is because the program pauses to wait for input there, after the program has just prompted you to enter a name. So you enter a name and the program prompts you to enter a number. Unfortunately, you're not going to get a chance to enter that number because when you get to line 41 there is still stuff in the input stream. If you entered "Smith\n" for example when prompted for a name, "mith\n" would still be in the input stream. This is not valid input for a number, so the stream is put into an error state and all subsequent attempts to extract input from the stream fail.
The correct thing to do is to elide all leading whitespace when you're going to do an unformatted extraction operation (such as getline.) A solution for this in the form of an istream manipulator is provided for you when you #include <iostream> . Remove the extraneous cin.get() calls and insert std::cin >> std::ws ; immediately before you call cin.getline.