So thanks to some help I have the following output file Teacher:Bob Marley Class
ID: 3669140 • Letter: S
Question
So thanks to some help I have the following output file
Teacher:Bob Marley
Class:CGS1010
Student Name: Jim Beam Average: 88 Grade: B
Student Name: Donna Jenn Average: 95 Grade: A
Student Count: 2
Student Average: 91.5
A's: 1
B's: 1
C's: 0
D's: 0
F's: 0
I should have
Teacher:Bob Marley
Class:CGS1010
Student Name: Jim Beam Average: 88 Grade: B
Donna Jenn 95 A
Student Count: 2
Student Average: 91.5
A's: 1
B's: 1
C's: 0
D's: 0
F's: 0
How can I make it output exactly what i need with out inclusinf the words "student name","average",Grade" for every person except for the first. The following is what I have so far. It is probably something simple.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void printFileToScreen();
char grade(float marks){
if(marks >=90)
return 'A';
else if(marks >=80)
return 'B';
else if(marks >=70)
return 'C';
else if(marks >=60)
return 'D';
else
return 'F';
}
int main()
{
//close the input file here
printFileToScreen();
cout<<"Data Saved Successfully!!!";
return 0;
}
void printFileToScreen()
{
char data[100];
int n;
float average;
float total = 0;
int k = 0;
float marks;
float totalAll = 0;
int a=0,b=0,c=0,d=0,f=0;
// open a file in write mode.
ofstream outfile;
outfile.open("class_statistics.txt");
cout << "Writing to the file" << endl;
cout << "Enter Teacher name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile <<"Teacher:"<<data << endl;
cout << "Enter Class name: ";
cin.getline(data, 100);
outfile <<"Class:"<<data << endl;
cout<<"Enter number of students: ";
cin>>n;
for(int i=1;i<=n;i++){
k = 0;
total = 0;
cout<<"Enter name of "<<i<<" student: ";
cin.ignore();
cin.getline(data, 100);
cout<<"Enter at most 10 grades of "<<i<<" student:[999 to stop] ";
for(int j=0; j<10;j++){
cin>>marks;
if(marks == 999)
break;
total = total + marks;
k++;
}
average = double(total)/k;
totalAll = totalAll + average;
char g = grade(average);
if(g=='A')
a++;
else if(g=='B')
b++;
else if(g=='C')
c++;
else if(g=='D')
d++;
else
f++;
outfile <<"Student Name: "<<data<<" ";
outfile <<"Average: "<<average<<" ";
outfile <<"Grade: "<<g<<endl;
}
outfile <<"Student Count: "<<n<<endl;
outfile <<"Student Average: "<<double(totalAll)/n<<endl;
outfile <<"A's: "<<a<<endl;
outfile <<"B's: "<<b<<endl;
outfile <<"C's: "<<c<<endl;
outfile <<"D's: "<<d<<endl;
outfile <<"F's: "<<f<<endl;
outfile.close();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void printFileToScreen();
char grade(float marks){
if(marks >=90)
return 'A';
else if(marks >=80)
return 'B';
else if(marks >=70)
return 'C';
else if(marks >=60)
return 'D';
else
return 'A';
}
int main()
{
//close the input file here
printFileToScreen();
cout<<"Data Saved Successfully!!!";
return 0;
}
void printFileToScreen()
{
char data[100];
int n;
float average;
float total = 0;
int k = 0;
float marks;
float totalAll = 0;
int a=0,b=0,c=0,d=0,f=0;
// open a file in write mode.
ofstream outfile;
outfile.open("class_statistics.txt");
cout << "Writing to the file" << endl;
cout << "Enter Teacher name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile <<"Teacher:"<<data << endl;
cout << "Enter Class name: ";
cin.getline(data, 100);
outfile <<"Class:"<<data << endl;
cout<<"Enter number of students: ";
cin>>n;
bool flag = false;
for(int i=1;i<=n;i++){
k = 0;
total = 0;
cout<<"Enter name of "<<i<<" student: ";
cin.ignore();
cin.getline(data, 100);
cout<<"Enter at most 10 grades of "<<i<<" student:[999 to stop] ";
for(int j=0; j<10;j++){
cin>>marks;
if(marks == 999)
break;
total = total + marks;
k++;
}
average = double(total)/k;
totalAll = totalAll + average;
char g = grade(average);
if(g=='A')
a++;
else if(a=='B')
b++;
else if(g=='C')
c++;
else if(g=='D')
d++;
else
f++;
if(!flag){
outfile <<"Student Name: "<<data<<" ";
outfile <<"Averag: "<<average<<" ";
outfile <<"Grade: "<<g<<endl;
flag = true;
}
else{
outfile <<" "<<data<<" ";
outfile <<" "<<average<<" ";
outfile <<" "<<g<<endl;
}
}
outfile <<"Student Count:"<<n<<endl;
outfile <<"Student Average:"<<double(totalAll)/n<<endl;
outfile <<"A':"<<a<<endl;
outfile <<"B':"<<b<<endl;
outfile <<"C':"<<c<<endl;
outfile <<"D':"<<d<<endl;
outfile <<"F':"<<f<<endl;
outfile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.