How to correct the output, because for the bold it follows every studentid.
/*------------------------------
Programming to coutn the examids
------------------------------*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct student
{
string studentid;
vector <int> examcode;
};
int main ()
{
int mycount;
{
ifstream stream1 ("STA83STU.txt");
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
vector <student> students;
student aStudent;
string tempStudentID;
bool readEntireFile = false; // set to true when reach end of file
stream1 >> tempStudentID; // read in student id of first student
while (!readEntireFile)
{
aStudent.studentid = tempStudentID; // new student
int tempExamCode;
aStudent.examcode.clear ();
stream1 >> tempExamCode; // read in first exam code for this student
aStudent.examcode.push_back (tempExamCode); // add this exam code to current student's vector of exam codes
bool newStudent = false; // true when a new student id is encountered
while (!newStudent && !readEntireFile)
{
if (stream1 >> tempStudentID) // successfully read in student id
{
if (tempStudentID.compare (aStudent.studentid) == 0) // student id is same as before
{
stream1 >> tempExamCode; // read in exam code
aStudent.examcode.push_back (tempExamCode); // add this exam code to this student;s vector of exam codes
}
else
newStudent = true; // student id is different from before. Therefore new student.
}
else
readEntireFile = true; // end of file reached. Want to exit inner and outer while loops
} // if new student, do not repeat this while loop
students.push_back (aStudent); // no more exam codes for this student. Add aStudent to students vector
}
stream1.close (); // We have read the entire file, so time to close it.
for (int i = 0; i < students.size (); i++)
{
cout << students.at (i).studentid << endl; // output student id
for (int j = 0; j < students.at (i).examcode.size (); j++)
cout << students.at (i).examcode.at (j) << "\t"; // output list of exam codes for this student
cout <<"\n"<<endl;
/*------------------------
counting elements in array
------------------------*/
[B] int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements
mycount = (int) count (myints, myints+8, 10);
cout << "10 appears " << mycount << " times.\n";[/B]/*-----------------------------
counting elements in container
-----------------------------*/
vector<int> myvector (myints, myints+8);
mycount = (int) count (myvector.begin(), myvector.end(), 20);
cout << "20 appears " << mycount << " times.\n";
}
return 0;
}
}