user1594399
user1594399

Reputation: 1

Reading and diaplaying a character array from a file in c++

I'm trying to write a program where a name and number of votes are read from a file. However I can't get the char array to read properly from the file.

void Output(char candidateLastName[][10], long votesRecieved[])
    {
         ifstream Electionr("Election.dat");
         int loop = 0;

         cout << left << setw(10) << "Candidate" << setw(5) << "Votes" << endl;

         Electionr >> candidateLastName[0][10];
         Electionr >> votesRecieved[0];

         cout << setw(10) << candidateLastName[0] << setw(5)
              << votesRecieved[0] << endl;

         for(int loop = 1; loop < 5; loop++)
         {
                 Electionr >> candidateLastName[0][10];
                 Electionr >> votesRecieved[loop];

                 cout << setw(10) << candidateLastName << setw(5)
                      << votesRecieved[loop] << endl;
         }

         Electionr.close();
    }

While the numbers from the file read properly, the characters will not.

Upvotes: 0

Views: 295

Answers (3)

paddy
paddy

Reputation: 63481

Electionr >> candidateLastName[0][10];

This is reading in a single character. Ignore for a moment that it's reading into the wrong location (the first character of the string at index 1)... I suspect that you want to do something like:

Electionr >> candidateLastName[0];

Also, I presume in your loop you want to use the loop variable instead of 0 to index the array. In that case, why did you not start your loop at zero and avoid duplicating that code?

for(int loop = 0; loop < 5; loop++)
{
    memset( &candidateLastName[loop], 0, 10 );

    Electionr >> candidateLastName[loop];
    Electionr >> votesRecieved[loop];

    cout << setw(10) << candidateLastName[loop] << setw(5)
         << votesRecieved[loop] << endl;
}

(I've also made a minor fix to the cout call above)

Be aware that you may need to explicitly null-terminate your strings (I forced that in my modified loop, but only if you happen to read 9 characters or less - otherwise you will overflow and have problems). I'm not sure if this is handled when reading into character arrays with the >> operator. I never do it for something like this. I use std::string instead.

Upvotes: 1

Antimony
Antimony

Reputation: 39501

First off you've got an out of bounds index on the array. Also, you're always writing to the first element. Also you shouldn't use raw arrays like that. IF you're going to use a fix sized array, at least use a std::array. But in this case std::string is most appropriate.

Anyway, it's hard to tell what you actually want, but here's my best guess at how to rewrite the code.

std::map<std::string, unsigned long> Output()
{
    std::map<std::string, unsigned long> data;
     ifstream Electionr("Election.dat");

     cout << left << setw(10) << "Candidate" << setw(5) << "Votes" << endl;

     while(Electionr.good()){
        std::string name;
        unsigned long votes = 0;

        getline(Electionr, name, ' ');
        Electionr >> votes;

        data[name] = votes;

        cout << setw(10) << name << setw(5)
          << votes << endl;
     }

     return data;
} 

Upvotes: 0

jxh
jxh

Reputation: 70502

Your first read should be:

         Electionr >> candidateLastName[0];

And within your loop, you want:

             Electionr >> candidateLastName[loop];

But, you are assuming each name will be at most 9 characters long (plus one for the null termination).

It would be safer to make your name array and array of std::string.

void Output(std::string candidateLastName[], long votesRecieved[])

Upvotes: 0

Related Questions