Can't read the first line of file!

So I finally got all this to work according to the prof. specification.
However when it outputs the second output skips the second name. I know its a memory problem, but I'm clueless as how to fix it.
Thanks.
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>

using namespace std;

const int NoCand = 5;//Global variable
const int StrSize = 20;//Global variable

int Input(char LastName[NoCand][StrSize], float NumVotes[NoCand],float &TotalVotes);
int Output(char LastName[NoCand][StrSize],float NumVotes[NoCand]);
int PercVotes(char LastName[NoCand][StrSize],float NumVotes[NoCand],float PercentVotes[NoCand], float &TotalVotes);

int main()
    {  
       char LastName[NoCand][StrSize];
       float NumVotes[NoCand];
       float PercentVotes[NoCand];
       float TotalVotes=0;
       char temp_LastName[20];
       float temp_NumVotes;
       Input(LastName,NumVotes,TotalVotes); 
       Output(LastName,NumVotes);
       PercVotes(LastName,NumVotes,PercentVotes,TotalVotes);
       system("pause");
       return 0;
    }
int Input(char LastName[NoCand][StrSize],float NumVotes[NoCand],float &TotalVotes)
    {  ofstream Election("Election.dat",ios::app);                             
       for(int LCV=0; LCV<NoCand; LCV++)
         { cout<<"\nEnter last name of candidate["<<LCV<<"]: ";
           cin.getline(LastName[LCV],StrSize); cin.get();
           cout<<"\nEnter votes received by candidate["<<LCV<<"]: ";
           cin>>NumVotes[LCV]; cin.get();
           TotalVotes = TotalVotes+NumVotes[LCV];
           Election<<LastName[LCV]<<'~'<<NumVotes[LCV]<<endl; 
         } Election.close();
    }
 int Output(char LastName[NoCand][StrSize], float NumVotes[NoCand])
    {  cout<<"Candidate's Last Name"<<"\t\t"<<"Votes Received"<<endl;
      ifstream Election("Election.dat");
      Election.getline(LastName[0],StrSize,'~');
      Election>>NumVotes[0];
      while(Election.good())
     { for(int LCV=0; LCV<NoCand; LCV++)
       { cout<<left<<setw(10)<<LastName[LCV]<<"\t\t\t"<<setw(8)<<NumVotes[LCV]<<endl;
         Election.getline(LastName[LCV],StrSize,'~');
         Election>>NumVotes[LCV];
       }
     } Election.close();
   }
      
      


int PercVotes(char LastName[NoCand][StrSize], float NumVotes[NoCand],float PercentVotes[NoCand],float &TotalVotes)
{   ofstream ElectionRep("ElectionRep.dat",ios::app);
    cout<<"\nCandidate's Last Name"<<"\t\t"<<"Votes Received"<<"\t\t% of total votes"<<endl;
    ElectionRep<<"Candidate's Last Name"<<"\t\t"<<"Votes Received"<<"\t\t% of total votes"<<endl;
    ifstream Election("Election.dat");
    Election.getline(LastName[0],StrSize,'~');
    Election>>NumVotes[0];
      while(Election.good())
     {for(int LCV=0; LCV<NoCand; LCV++)
     {PercentVotes[LCV] = (NumVotes[LCV]/TotalVotes)*100; 
     cout<<left<<setw(10)<<LastName[LCV]<<"\t\t\t"<<setw(8)<<NumVotes[LCV]<<"\t\t"<<PercentVotes[LCV]<<endl;  
     ElectionRep<<left<<setw(10)<<LastName[LCV]<<"\t\t\t"<<setw(8)<<NumVotes[LCV]<<"\t\t"<<PercentVotes[LCV]<<endl;    
     Election.getline(LastName[LCV],StrSize,'~');
     Election>>NumVotes[LCV];  }
     

    }Election.close();

    float temp_NumVotes;
    char temp_LastName[25];
       
    for(int i=0;i<NoCand;i++) 
       {for(int j=i+1;j<NoCand;j++) 
        {
       if(NumVotes[j]>NumVotes[i]) {
	            strcpy( temp_LastName, LastName[i]);
	            float temp_NumVotes = NumVotes[i];
	            strcpy( LastName[i], LastName[j]);
	            NumVotes[i] = NumVotes[j];
	            strcpy( LastName[j], temp_LastName); 
	            NumVotes[j] = temp_NumVotes;
	        }
	    }
        }
            
	  int k = 0;
	      cout<<"Total Votes \t\t" << TotalVotes<<endl;
	      ElectionRep<<"Total Votes \t\t" << TotalVotes<<endl;
	    cout << "\nThe Winner of the election is " << LastName[k]<<endl;
	 ElectionRep<< "\nThe Winner of the election is " << LastName[k]<<endl;
	 ElectionRep.close();
	 }.


Candidate's Last Name           Votes Received
Johnson                         5000
Miller                          4000
Duffy                           6000
Robinson                        2500
Ashtony                         1800

Candidate's Last Name           Votes Received          % of total votes
Johnson                         5000                    25.9067

Duffy                           6000                    31.0881

Robinson                        2500                    12.9534

Ashtony                         1800                    9.32642

                                1800                    9.32642
Total Votes             19300

The Winner of the election is Duffy
closed account (28poGNh0)
Couple of remarks

1: You dont use temp_LastName[20] and temp_NumVotes so delete them.
2: Functions input , output and PercVotes,do not return values so return them to void.
3: In function input you need cin.get() only once after cin statement,so remove the first one.
4: You should indente and well positionning your code more your code is clear more you'dd be better programmer

finaly you can miss with this new version of your code

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cstring>
# include <cstdlib>// for system("pause");
using namespace std;

const int NoCand = 5;//Global variable
const int StrSize = 20;//Global variable

void Input(char LastName[NoCand][StrSize], float NumVotes[NoCand],float &TotalVotes);
void Output(char LastName[NoCand][StrSize],float NumVotes[NoCand]);
void PercVotes(char LastName[NoCand][StrSize],float NumVotes[NoCand],
               float PercentVotes[NoCand],float &TotalVotes);

int main()
{
    char LastName[NoCand][StrSize];
    float NumVotes[NoCand],PercentVotes[NoCand],TotalVotes=0;

    Input(LastName,NumVotes,TotalVotes);
    Output(LastName,NumVotes);
    PercVotes(LastName,NumVotes,PercentVotes,TotalVotes);

    system("pause");// Dont use it

    return 0;
}

void Input(char LastName[NoCand][StrSize],float NumVotes[NoCand],float &TotalVotes)
{
    ofstream Election("Election.dat",ios::app);

    for(int LCV=0; LCV<NoCand; LCV++)
    {
        cout << "Enter last name of candidate[" << LCV << "]     : ";
        cin.getline(LastName[LCV],StrSize);

        cout << "Enter votes received by candidate[" << LCV << "]: ";
        cin >> NumVotes[LCV];cin.get();

        cout << endl;//Just for a bit of organization
        TotalVotes = TotalVotes + NumVotes[LCV];
        Election << LastName[LCV] << '~' << NumVotes[LCV] << endl;
    }Election.close();
}

void Output(char LastName[NoCand][StrSize], float NumVotes[NoCand])
{
    cout << "Candidate's Last Name" << "\t\t" << "Votes Received" << endl;
    ifstream Election("Election.dat");

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

        cout << left << setw(10) << LastName[LCV] << "\t\t\t" << setw(8) << NumVotes[LCV] << endl;
    }Election.close();
}

void PercVotes(char LastName[NoCand][StrSize], float NumVotes[NoCand],
               float PercentVotes[NoCand],float &TotalVotes)
{
    ofstream ElectionRep("ElectionRep.dat",ios::app);
    cout << "\nCandidate's Last Name" << "\t\t" << "Votes Received"
    << "\t\t% of total votes" << endl;

    ElectionRep << "Candidate's Last Name" << "\t\t" << "Votes Received"
    << "\t\t% of total votes" << endl;

    ifstream Election("Election.dat");
    Election.getline(LastName[0],StrSize,'~');
    Election >> NumVotes[0];

    for(int LCV=0; LCV<NoCand&&Election.good(); LCV++)
    {
        PercentVotes[LCV] = (NumVotes[LCV]/TotalVotes)*100;
        cout << left << setw(10) << LastName[LCV] << "\t\t\t" << setw(8)
        << NumVotes[LCV] << "\t\t" << PercentVotes[LCV] << endl;

        ElectionRep << left << setw(10) << LastName[LCV] << "\t\t\t" << setw(8)
        << NumVotes[LCV] << "\t\t" << PercentVotes[LCV] << endl;

        Election.getline(LastName[LCV],StrSize,'~');
        Election >> NumVotes[LCV];
    }Election.close();

    // float temp_NumVotes;Unused variable
    char temp_LastName[25];

    for(int i=0;i<NoCand;i++)
    {
        for(int j=i+1;j<NoCand;j++)
            if(NumVotes[j]>NumVotes[i])
            {
                strcpy( temp_LastName, LastName[i]);
                float temp_NumVotes = NumVotes[i];
                strcpy( LastName[i], LastName[j]);
                NumVotes[i] = NumVotes[j];
                strcpy( LastName[j], temp_LastName);
                NumVotes[j] = temp_NumVotes;
            }
    }

    int k = 0;
    cout << "Total Votes \t\t" << TotalVotes << endl;
    ElectionRep << "Total Votes \t\t" << TotalVotes << endl;
    cout << "\nThe Winner of the election is " << LastName[k] << endl;
    ElectionRep << "\nThe Winner of the election is " << LastName[k]<< endl;
    ElectionRep.close();
}


Thanks,

I have another question related to the final out put.
At the end when the program outputs LastName[k] why does it do it on a separate line?

Other than that thankyou so much.
closed account (28poGNh0)
You're welcome anytime

thats because LastName[k] reads a newline too, so put those codes at the line 75

1
2
char newline[3];
    Election.getline(newline,StrSize);


hope that helps
Last edited on
Ok, So I followed many of your tips from the previous program but am running into the same issue where it skips data points before it starts reading. This is happening in the third function. The output is below and you'll see what I mean.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<fstream>

using namespace std;

void Input(int Score[5][4],int ID[5]);
void Output(int Score[5][4], int ID[5]);
void Avg(int Score[5][4],int ID[5], int AvgOne[5],int AvgTwo[5]);
//int LetterGrade(int Score[5][4], int AvgOne,int AvgTwo, char LGrade1, char LGrade2);

int main()
{
    int Score[5][4];
    int AvgOne[5];
    int AvgTwo[5];
    int ID[5];
    char GradeWeighted[5];
    char GradeAvg[5];
    char LGrade1;
    char LGrade2;
    
    Input(Score,ID);
    Output(Score,ID);
    Avg(Score,ID,AvgOne,AvgTwo);
    system("pause");
    return 0;
   
}

void Input(int Score[5][4],int ID[5])
{ ofstream Student("Student.dat",ios::app);
    for(int i=0;i<5;i++)
        {cout<<"\nEnter ID of Student["<<i<<"]: ";
         cin>>ID[i];
         Student<<ID[i]<<" ";
         for(int j=0;j<4;j++)
             {cout<<"\nEnter Test Score["<<j<<"] of Student["<<i<<"]: ";
              cin>>Score[i][j];
              Student<<Score[i][j]<<" ";}
         Student<<endl;}
    Student.close();
}

void Output(int Score[5][4], int ID[5])
   {
    cout<<"StudID\tScore 1\tScore 2\tScore 3\tScore 4\t"<<endl;
    ifstream Student("Student.dat");
    Student>>ID[0]>>Score[0][0];
    for(int i=0;i<5&&Student.good();i++)
       {
            cout <<ID[i]<<"\t";
            Student>>ID[i];
            for(int j=0;j<4&&Student.good();j++)
            {
                    cout<<Score[i][j]<<" ";
                    Student>>Score[i][j];
            }
            cout<<endl;
        }
       
       Student.close();
}

void Avg(int Score[5][4],int ID[5], int AvgOne[5],int AvgTwo[5])
     { 
      cout<<"StudID\tScore 1\tScore 2\tScore 3\tScore 4\tAvg1\tAvg2"<<endl;
      ifstream Student("Student.dat");
      Student>>ID[0]>>Score[0][0];
      for(int i=0;i<5&&Student.good();i++)
        {
              cout <<ID[i]<<"\t";
              Student>>ID[i];
              for(int j=0;j<4&&Student.good();j++)
              {
                      cout<<Score[i][j]<<" ";
                      Student>>Score[i][j];
              }                      
              AvgOne[i]=(Score[i][1]+Score[i][2]+Score[i][3]+Score[i][4])*.25;
              AvgTwo[i]=(0.2*(Score[i][1])+0.3*(Score[i][2])+0.3*(Score[i][3])+0.2*(Score[i][4]))*25;
              cout<<AvgOne[i]<<" "<<AvgTwo[i];
              cout<<endl;
         }
         Student.close();
       
       }




StudID  Score 1 Score 2 Score 3 Score 4
1       100 100 100 100
2       100 0 100 0
3       82 94 73 86
4       64 74 84 94
5       94 84 74
StudID  Score 1 Score 2 Score 3 Score 4 Avg1    Avg2
1       100 100 2 100 75 1765
0       100 0 3 82 39 1002
94      73 86 4 64 59 1360
74      84 94 5 94 66 1582
84      74 54 74 671743 13435190
Press any key to continue . . .
Topic archived. No new replies allowed.