What's wrong with my coding???

My coding won't read next person in the data file. It's only read, calculate, and print out first student in the data file.Please tell me what i did wrong on my code. Thank you

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std ;
/*
call function open input file
*/
bool Open_input_file(ifstream &fin);
bool Open_output_file(ofstream &);
int main(){
ifstream Input ;
ofstream Output ;
string Name;
char Grade ;
int Units,Counter=0;
double Total =0,gpa,Average,Sum = 0,count;
long StudentID;

if(Open_input_file(Input)== true)
{
if(Open_output_file(Output) == true)
{
getline(Input,Name);
if(!Input.eof())
{
Input >> StudentID;
if(!Input)
{
cout << "Bad StudentID!!!!" << Name << endl;
}
Input >> Grade >> Units;
while(Grade != '*')
{
//1
switch(Grade)
{
case 'a':
case 'A':
gpa = 4.0;
break;
case 'b':
case 'B':
gpa = 3.0;
break;
case 'c':
case 'C':
gpa = 2.0;
break;
case 'd':
case 'D':
gpa = 1.0;
break;
case 'f':
case 'F':
gpa = 0.0;
break;
default:
Units = 0;
gpa = 0;
}//1

//2

switch(Units)
{
case 1:
count = gpa * Units;
break;
case 2:
count = gpa * Units;
break;
case 3:
count = gpa * Units;
break;
case 4:
count = gpa * Units;
break;
case 5:
count = gpa * Units;
break;
default:
Units = 0;
count = 0;
}// 2

//3 check units
if(gpa <= 0)
{
Units = 0;
Input >> Grade >> Units;
}
if(Units > 0 && Units < 5)
{
Input >> Grade >> Units;
}

else
{
Units = 0;
Input >> Grade >> Units;
}
Sum += Units;
Total += count;
Counter ++;
}//end loop while grade == '*'
if(Counter > 0)
Average = Total/Sum;
else
Average = 0;

cout << left << setprecision(2) << fixed ;
cout << left << setw(20) << "Name" << setw(20) << "ID#" << setw(20) << "Units" << setw(20) << "GPA" ;
Output << "\n" << setw(20) << Name << setw(20) << StudentID << setw(20) << Sum << setw(20) << Average << endl;
cout << setw(20) << Name << setw(20) << StudentID << setw(20) << Sum << setw(20) << Average;
Input.ignore(10,'\n');

getline(Input,Name);
}

}
else
cout <<"\nNo outputfile.\n";
}
else
cout <<"Inputput fiel did not open."<<
"No data to process. Program terminated.\n";

return 0 ;
}


bool Open_input_file(ifstream &fin){
string file_name ;
cout <<"Enter input file name and location: ";
cin >> file_name ;
fin.open(file_name.c_str());
if (fin.fail())
return false ;
else
return true ;
}
bool Open_output_file(ofstream &fout){
string file_name ;
cout <<"Enter output file name and location: ";
cin >> file_name ;
fout.open(file_name.c_str());
if (fout.fail())
return false ;
else
return true ;
}


Also this is my data file:
Smith, John
12345678 A 3 b 4 b 2 x 4 C 4 *
Brown Jones, Nancy
22345678 a 5 B 4 a 3 B 4 *
Adamson Jr., Jerry
99887766 A 5 B 4 a 3 b 4 *
Shore, John
12345668 A 3 b 4 b 2 b 4 C 4 *
Jonsen, Lori
22445678 a 4 C 4 D 3 B 4 *
Blackston Jr., Jerry
99887666 A 5 K 4 D 3 A 4 *
Partison, John
12346678 A 2 b 3 h 5 x 4 C 4 *
Brownson, Adam
22345688 a 3 C 4 b 2 C 4 *
Johnson, Jerry
99877766 A 5 B 4 B 3 C 4 *
If this is where you input one student data:

1
2
3
4
getline(Input,Name);
if(!Input.eof())
{
Input >> StudentID;


that's not inside a loop, so of course it only happens once.
The first thing you want to do is clean up your code like this

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
int main() {
  ifstream Input ;
  ofstream Output ;
  string Name;
  char Grade ;
  int Units,Counter=0;
  double Total =0,gpa,Average,Sum = 0,count;
  long StudentID;

  if(Open_input_file(Input)== true) {
    if(Open_output_file(Output) == true) {
      getline(Input,Name);
      
      if(!Input.eof()) {
        Input >> StudentID;
        
        if(!Input)
          cout << "Bad StudentID!!!!" << Name << endl;
      }
      
    Input >> Grade >> Units;
    
    while(Grade != '*') {
//1
      switch(Grade) {
        case 'a':
        case 'A':
	  gpa = 4.0;
          break;
          
        case 'b':
        case 'B':
          gpa = 3.0;
          break;


Now you can readily see two problems. Input was established when you opened the file, so how is it ever going to be false, unless you close the file. You probably wanted to check StudentID and then if there is a problem, you just merily carry on.

As you can tell, your program compiled properly and therefore the compiler doesn't really care what it looks like, but humans do. It's a lot easier to conceptualize the program especially using indentation. Once you've got it cleaned up. Let's have a look again and I'll bet all problems will stand right out
Topic archived. No new replies allowed.