ofstream & Array& Function

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<fstream>


using namespace std;

const int NumCandidates=5;//Global variable
const int StringSize=20;//Global variable

int Input(char LastName[NumCandidates][StringSize],
long NumVotes[NumCandidates]);

int Output(char LastName[NumCandidates][StringSize],
long NumVotes[NumCandidates]);


int main()
{   int x;
    char LastName[NumCandidates][StringSize];
    long NumVotes[NumCandidates];
    float PercentVotes[NumCandidates];
    cin>>x;
    if(x!=1)
    Input(LastName,NumVotes);  
    else
    {Output(LastName,NumVotes);
    PercVotes(LastName,NumVotes,PercentVotes);} 
    system("pause");
    return 0;
}

int Input(char LastName[NumCandidates][StringSize],
long NumVotes[NumCandidates])
{
                              
 ofstream Election("Election.dat",ios::app);                             
 for(int LCV=0; LCV<NumCandidates; LCV++)
 { cout<<"\nEnter the last name of candidate["<<LCV<<"]: ";
   cin.getline(LastName[LCV],StringSize); cin.get();
   cout<<"\nEnter the votes received by candidate["<<LCV<<"]: ";
   cin>>NumVotes[LCV]; cin.get();
   Election<<LastName[LCV]<<'~'<<NumVotes[LCV]<<endl; 
   }
  
}

int Output(char LastName[NumCandidates][StringSize],
long NumVotes[NumCandidates])
{
    cout<<"Candidate's Last Name"<<"\t\t"<<"Votes Received"<<endl;
    ifstream Election("Election.dat");
    for(int LCV=0; LCV<NumCandidates; LCV++)
    { Election.getline(LastName[LCV],'~');
      Election>>NumVotes[LCV];
    while(Election.good())
    { Election.getline(LastName[LCV],'~');
      Election>>NumVotes[LCV];
      cout<<LastName[LCV]<<"\t\t"<<NumVotes[LCV]<<endl;}}}
      
Still struggling with this. Any help will be most appreciated.
What's the purpose of the extra cin.get(); calls? You should remove them.
Apart from the I/O error, your code can be improved in several ways.

1) Adopt a sane and logical indentation style, that increases the readability of your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int Output(char LastName[NumCandidates][StringSize], long NumVotes[NumCandidates])
{
    cout << "Candidate's Last Name" << "\t\t" << "Votes Received" << endl;

    ifstream Election("Election.dat");

    for (int LCV=0; LCV < NumCandidates; LCV++)
    {
        Election.getline(LastName[LCV], '~');
        Election >> NumVotes[LCV];

        while (Election.good())
        {
            Election.getline(LastName[LCV], '~');
            Election >> NumVotes[LCV];
            cout << LastName[LCV] << "\t\t" << NumVotes[LCV] << endl;
        }
    }
}


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.

Read these, maybe:
http://www.cplusplus.com/doc/tutorial/structures/
http://www.cplusplus.com/reference/stl/
Last edited on
I'm attempting to help my girlfriend with her homework so according to the assignment were not allowed to use structs.


What I'm finding when I add the if else in the main is that it only reads the first line of the data file and give a bunch of nonsense.

I think the issue is with the multidimensional character array, but I don't know how to fix it.
Last edited on
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>

using namespace 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? 
Last edited on
I wish must use char. Everything in these assignments are the hard way. Thanks for the help though.
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.
cire,

Thanks for the information regarding my int x;. I was using this to shortcut to the out put function but wasn't working thanks.
Topic archived. No new replies allowed.