Alright, I will do my best to ask this kind of question. This is a homework assignment, FYI so I gotta learn the hard way. I'm in a class that we are learning C++11. It is basically new to me so I'm overwhelmed with its features. However, this assignment expects us to print out users' IDs along with their "exam scores" and calculate its average score of the total of 3 exams from a text file. FYI: I prefer to focus on reading characters first.
Alright, I have questions to ask
Since I wrote the double in class for average number, do I need to write a separate function to do the math for average score of 3 exams?
I was asked to modify the constructor so you can pass in the values for initialization and create 3 different students, output each one. Do I need to write a range for loop in main() with it?
I was asked to create a vector of students, and push those 3 students into the vector and loop over the vector and output each one. I created vector studentInfo, but is for_each algorithm correct?
Ultimately, is my ifstream code done correctly??
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
string score1("100");
int exam1 = atoi(score1.c_str());
string score2("100");
int exam2 = atoi(score2.c_str());
string score3("100");
int exam3 = atoi(score3.c_str());
class student
{
public:
string netid;
int exam1;
int exam2;
int exam3;
student(string name) // do i need to add another argument
{
netid = "AAA";
exam1 = 100;
exam2 = 100;
exam3 = 100;
}
double avg()
{
return ((exam1 + exam2 + exam3) / 3.0);
}
};
int main()
{
ifstream file("students.txt");
istream_iterator<string> start(file), end;
vector<student> studentInfo;
vector<int> score;
for_each(start, end, [&](string s)
{
studentInfo.push_back(s);
});
for (student &s : studentInfo)
{
cout << s.netid << endl;
}
for (int &x : score)
{
cout << x.exam1 << endl;
}
system("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire