C++ array implementation

This code here is what i have so far. What i need to do is to store the data of file(which is all the way in the bottom ) in an array of [20].
Can someone please help me. I am very stuck..
and if you are wondering why i did a structure first and then the array is because that is how the professor told us to do. i am using functions by the way..i am also required to do so.





#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Student{
string Name;
long ID;
char Grade;
int Total_points, Total_Units, Group_Units, Units, counter;
float GPA, Group_GPA, Score, points;
};

//Prototypes
void Process_Student(Student &Temp, ifstream & Fin);
void Display_Header(void);
bool Open_File( ifstream &in );
bool Open_File( ofstream &out );
float Assign_GPA( Student & Temp);
void Process(Student & Temp);
bool Verify (Student & Temp);

int main()
{
//Declare Variables
Student S1 = {"", 0, '\0', 0, 0, 0, 0, 0, 0, 0, 0, 0};// initializing the structure..Notice the order of all the zeros.
ifstream Fin ;
ofstream Fout ;

Display_Header();

if(Open_File(Fin))
{
if (Open_File(Fout))
{
Fout<< setprecision(2)<<fixed << showpoint ;
cout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
cout <<"====================================================\n";
Fout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
Fout <<"====================================================\n";
getline (Fin, S1.Name);
while(!Fin.eof())
{
Fin>> S1.ID ;
Process_Student(S1, Fin);
cout << setw(25) << S1.Name << setw(10) << S1.ID << setw(10) << S1.Total_Units << setw(8) << S1.GPA <<endl;
Fout << setprecision(2)<<fixed;
Fout << setw(25) << S1.Name << setw(10) << S1.ID << setw(10) << S1.Total_Units << setw(8) << S1.GPA <<endl;
S1.counter++ ;
Fin.ignore(10,'\n');
getline(Fin, S1.Name);
S1.Total_Units = 0;
S1.GPA = 0;

}
S1.Group_GPA /= S1.Group_Units ;
cout <<"==================================================\n";
cout <<"Group of "<<S1.counter<<setw(25)<<" Students Totals" <<setw(10)<< S1.Group_Units << setw(10)<< S1.Group_GPA << endl << endl ;
Fout <<"==================================================\n\n";
Fout <<"Group of "<<S1.counter<<setw(25)<<" Students Totals" <<setw(10)<< S1.Group_Units << setw(10)<< S1.Group_GPA << endl << endl ;
cout << endl << endl ;
}
}
return 0;
}

//===================================================================================================================================
void Display_Header(void)
{
cout<< setw(45)<< right<< "Sample Soultion"<< endl;
cout<< setw(45)<< right<< " CSCI 1 "<< endl;
cout<< setw(45)<< right<< " Assignment 6 "<< endl<< endl;
cout<< setprecision(2)<<fixed << showpoint ;
}

void Process_Student(Student & Temp, ifstream & Fin)
{


Fin >> Temp.Grade;
while(Temp.Grade != '*')
{
Fin >> Temp.Units;
Temp.Score = Assign_GPA(Temp);
Process(Temp);
Fin>> Temp.Grade;
}
Temp.Group_Units += Temp.Total_Units ;
Temp.Group_GPA += Temp.GPA ;
if( Temp.Total_Units > 0 )
Temp.GPA/= Temp.Total_Units;

}
bool Open_File( ifstream &in )
{
int counter = 0 ;
string File_name ;
do{
counter++ ;
in.clear();
cout<< "Enter the input file name: ";
getline(cin, File_name);
in.open(File_name.c_str());
}while (in.fail() && counter < 3);
if(counter > 3)
{
cout <<"Bad file name.\nProgram Terminated.\n\n";
return false;
}
return true ;
}

bool Open_File( ofstream &out )
{
int counter = 0 ;
string File_name ;

do{
counter++ ;
out.clear();
cout<< "Enter the output file name: ";
getline(cin, File_name);
out.open(File_name.c_str());
}while (out.fail() && counter < 3);
if(counter > 3)
{
cout <<"Bad file name.\nProgram Terminated.\n\n";
return false;
}
return true ;
}

float Assign_GPA( Student & Temp)
{
if(Temp.Grade == 'A' || Temp.Grade=='a')
Temp.points=4.0;
else if(Temp.Grade == 'B' || Temp.Grade == 'b')
Temp.points=3.0;
else if(Temp.Grade == 'C' || Temp.Grade == 'c')
Temp.points=2.0;
else if(Temp.Grade == 'D' || Temp.Grade == 'd')
Temp.points=1.0;
else if(Temp.Grade =='F' || Temp.Grade =='f')
Temp.points=0.0 ;
else
Temp.points=-1;
return Temp.points ;
}

void Process(Student & Temp)
{
if (Verify(Temp) && Temp.points >= 0)
{
Temp.GPA = (Temp.GPA+Temp.points*Temp.Units);
Temp.Total_Units += Temp.Units;
}
}

bool Verify (Student & Temp)
{
if (Temp.Units > 0 && Temp.Units <=5)
return true;
else
return false ;
}




<><><><><><><><><><><><><><><><><><><><><><><><>><><>
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 *
Bad Data2, Joe
88887777 X 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 *
Bad Data, Joe
22223333 *
Partison, John
12346678 A 2 b 3 h 5 x 4 C 4 *
Brownson, Adam
22345688 a 3 C 4 b 2 C 4 *
Bad Data1, Joe
99999999 A 7 *
Johnson, Jerry
99877766 A 5 B 4 B 3 C 4 *

[/b]
I see this is your first post. Welcome to the forum.
I personally can't help because (and this may very well be why you didn't receive other replies so far):

1) Your code is not formatted. You have to use [code][/code] tags to preserve indentation and provide highlighting.
2) You didn't ask a specific question. Did you even write this code yourself?
Yes I wrote it myself. I honestly don't understand what you meant by format it.
And well what i am asking is if you know how to put the data in the input file that is at the very bottom in an array of [20].
Thank you for trying!
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

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Student{
string Name;
long ID;
char Grade;
int Total_points, Total_Units, Group_Units, Units, counter;
float GPA, Group_GPA, Score, points;
};

//Prototypes
void Process_Student(Student &Temp, ifstream & Fin);
void Display_Header(void);
bool Open_File( ifstream &in );
bool Open_File( ofstream &out );
float Assign_GPA( Student & Temp);
void Process(Student & Temp);
bool Verify (Student & Temp);

int main()
{
//Declare Variables
Student S1 = {"", 0, '\0', 0, 0, 0, 0, 0, 0, 0, 0, 0};// initializing the structure..Notice the order of all the zeros.
ifstream Fin ;
ofstream Fout ;

Display_Header();

if(Open_File(Fin))
{
if (Open_File(Fout))
{
Fout<< setprecision(2)<<fixed << showpoint ;
cout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
cout <<"====================================================\n";
Fout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
Fout <<"====================================================\n";
getline (Fin, S1.Name);
while(!Fin.eof())
{
Fin>> S1.ID ;
Process_Student(S1, Fin);
cout << setw(25) << S1.Name << setw(10) << S1.ID << setw(10) << S1.Total_Units << setw(8) << S1.GPA <<endl;
Fout << setprecision(2)<<fixed;
Fout << setw(25) << S1.Name << setw(10) << S1.ID << setw(10) << S1.Total_Units << setw(8) << S1.GPA <<endl;
S1.counter++ ;
Fin.ignore(10,'\n');
getline(Fin, S1.Name);
S1.Total_Units = 0;
S1.GPA = 0;

}
S1.Group_GPA /= S1.Group_Units ;
cout <<"==================================================\n";
cout <<"Group of "<<S1.counter<<setw(25)<<" Students Totals" <<setw(10)<< S1.Group_Units << setw(10)<< S1.Group_GPA << endl << endl ;
Fout <<"==================================================\n\n";
Fout <<"Group of "<<S1.counter<<setw(25)<<" Students Totals" <<setw(10)<< S1.Group_Units << setw(10)<< S1.Group_GPA << endl << endl ;
cout << endl << endl ;
}
}
return 0;
}

//===================================================================================================================================
void Display_Header(void)
{
cout<< setw(45)<< right<< "Sample Soultion"<< endl;
cout<< setw(45)<< right<< " CSCI 1 "<< endl;
cout<< setw(45)<< right<< " Assignment 6 "<< endl<< endl;
cout<< setprecision(2)<<fixed << showpoint ;
}

void Process_Student(Student & Temp, ifstream & Fin)
{


Fin >> Temp.Grade;
while(Temp.Grade != '*')
{
Fin >> Temp.Units;
Temp.Score = Assign_GPA(Temp);
Process(Temp);
Fin>> Temp.Grade;
}
Temp.Group_Units += Temp.Total_Units ;
Temp.Group_GPA += Temp.GPA ;
if( Temp.Total_Units > 0 )
Temp.GPA/= Temp.Total_Units;

}
bool Open_File( ifstream &in )
{
int counter = 0 ;
string File_name ;
do{
counter++ ;
in.clear();
cout<< "Enter the input file name: ";
getline(cin, File_name);
in.open(File_name.c_str());
}while (in.fail() && counter < 3);
if(counter > 3)
{
cout <<"Bad file name.\nProgram Terminated.\n\n";
return false;
}
return true ;
}

bool Open_File( ofstream &out )
{
int counter = 0 ;
string File_name ;

do{
counter++ ;
out.clear();
cout<< "Enter the output file name: ";
getline(cin, File_name);
out.open(File_name.c_str());
}while (out.fail() && counter < 3);
if(counter > 3)
{
cout <<"Bad file name.\nProgram Terminated.\n\n";
return false;
}
return true ;
}

float Assign_GPA( Student & Temp)
{
if(Temp.Grade == 'A' || Temp.Grade=='a')
Temp.points=4.0;
else if(Temp.Grade == 'B' || Temp.Grade == 'b')
Temp.points=3.0;
else if(Temp.Grade == 'C' || Temp.Grade == 'c')
Temp.points=2.0;
else if(Temp.Grade == 'D' || Temp.Grade == 'd')
Temp.points=1.0;
else if(Temp.Grade =='F' || Temp.Grade =='f')
Temp.points=0.0 ;
else
Temp.points=-1;
return Temp.points ;
}

void Process(Student & Temp)
{
if (Verify(Temp) && Temp.points >= 0)
{
Temp.GPA = (Temp.GPA+Temp.points*Temp.Units);
Temp.Total_Units += Temp.Units;
}
}

bool Verify (Student & Temp)
{
if (Temp.Units > 0 && Temp.Units <=5)
return true;
else
return false ;
}


That's what he meant.

You basically you put your code inside between the [code][/code]


Anyhow, this code looks huge, it will take me a while to look and know what the problem is (Meaning when is this program due?) Were you having any build errors?
Last edited on
Topic archived. No new replies allowed.