OOP with file I/O

The project is to take code like

2
Chrysafis Vogiatzis 90 90 80 100 90 100 89 92.5 82
Abrar Polani 100 100 100 100 100 100 96 98 93

and turn it into

Chrysafis Vogiatzis A-
Abrar Polani A

After I type in the name of the write file, my compiler just gets stuck. Help would be greatly appreciated. My code is below.

#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
#include<cstring>

using namespace std;

float temp2[20];
string temp1;
ofstream file2;
int count;
float sum=0;
float avg;
string temp3;

class average{

private:

string name;
string grade;
string name2;

public:

average();
average(string, string, int);

string get_name();
string get_grade();
string get_name2();

void set_name(string);
void set_grade(int count, float temp2[]);
void set_name2(string);

};

average::average()
{
name = "student";
grade = "F";
}

string average :: get_name()
{
return name;
}

string average :: get_name2()
{
return name2;
}

string average :: get_grade()
{
return grade;
}

void average :: set_grade(int count, float temp2[])
{
for (int j=0; j<count; j++){
sum+=temp2[j];}
avg=sum/count;
if (avg>=93){grade="A";}
else if (avg>=87){grade="A-";}
else if (avg>=83){grade="B+";}
else if (avg>=80){grade="B";}
else if (avg>=77){grade="B-";}
else if (avg>=73){grade="C+";}
else if (avg>=70){grade="C";}
else if (avg>=67){grade="C-";}
else if (avg>=63){grade="D+";}
else if (avg>=60){grade="D";}
else if (avg>=57){grade="D-";}
else {grade="F";}
}

void average :: set_name(string temp1)
{
name=temp1;
}

void average :: set_name2(string temp3)
{
name2=temp3;
}

int main()
{
int choice;
ifstream file;
char fname[40];
char outname[40];
bool option1=false;
average *student;
int num;
int j=0;
bool end=false;
bool done=false;
int k=0;

loop1:
cout << "Classroll Manager\n";
cout << "1. To input classroll\n";
cout << "2. To create grade file\n";
cout << "Press 0 to quit\n";
cout << "Please choose: ";
cin >> choice;

if (choice==1){

cin.get();
cout << "Please give file name: ";
cin.getline(fname, sizeof(fname));
file.open(fname);

if (file.fail()) {
cout << "Oops! File does not exist.\n";
file.clear();
goto loop1;}

else{
file >> num;
student = new average[num];
cout << "Database loaded successfully!\n";
option1 = true;
goto loop1;}
}

else if (choice==2){

if (option1 == false){
cout << "You have to load a database first.\n";
goto loop1;}

else{
cout << "Please give name for the grade file: ";
cin.ignore();
cin.getline(outname, sizeof(outname));}

for(int i = 0; i<num; i++){
file >> temp1;
student[i].set_name(temp1);
file >> temp3;
student[i].set_name(temp3);
temp3.clear();
temp1.clear();
do{
file >> temp2[j];
if (file.eof()){
end=true;
int count=j;
student[i].set_grade(count, &temp2[j]);}

else if (file.fail()){
done=true;
int count=j;
student[i].set_grade(count, &temp2[j]);}

else{
j++;}
}while((end==false)||(done==false));
}

file2.open(outname);
for(int i=0; i<num; i++){
file2 << student[i].get_name();
file2 << " ";
file2 << student[i].get_name2();
file2 << " ";
file2 << student[i].get_grade();
file2 << "\n";}
goto loop1;
}
if (choice==0){
cout << "Now quitting..";
system("PAUSE");
return 0;}

else {
cout << "Wrong choice. Please choose again.\n";
goto loop1;}

}
Last edited on
It would help to know what exactly isn't working so we know where to look for an issue.
Yeah, I need to know what the problem is...

But just looking at your code I found a few beginner mistakes. First of all, you should never-ever use a go-to statement. It's been deprecated in C++, and it leads to bad programming habits. And You might want to take out system("PAUSE"), I hear it causes issues with programs.... you should use something like cin.ignore() instead if your looking to hold the console window open.
Last edited on
System("pause") is inefficient, and is a big security risk. There's some good articles out there on why. Why people still teach it, no one will ever know.
Topic archived. No new replies allowed.