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;
}
}

Dani AI

Generated

Short answer: the bolded count code is inside the loop that prints each student, so it runs once per student. Cut that counting block out of the student-print loop and place it after the loop (or, if you intended counts per student, compute them inside the loop but using each student's vector). As suggested, showing input format would help, but the immediate problem is placement.

Example (move counting outside the student loop):

for (size_t i = 0; i < students.size(); ++i) {
    cout << students[i].studentid << '\n';
    for (size_t j = 0; j < students[i].examcode.size(); ++j)
        cout << students[i].examcode[j] << '\t';
    cout << '\n';
}

// counting runs once, after all students have been printed
int values[] = {10,20,30,30,20,10,10,20};
size_t n = sizeof(values) / sizeof(values[0]);
int cnt10 = std::count(values, values + n, 10);
cout << "10 appears " << cnt10 << " times\n";

If the goal is to get counts per student (rather than a single global count), use std::count on the student's examcode vector or build a frequency map while reading:

std::map<int,int> freq;
for (size_t i = 0; i < students.size(); ++i)
  for (size_t j = 0; j < students[i].examcode.size(); ++j)
    ++freq[students[i].examcode[j]];
cout << "20 appears " << freq[20] << " times\n";

Extra tips: check the initial stream >> tempStudentID in case the file is empty; avoid cout << "\n" << endl; (redundant — use one or the other); and prefer size_t/auto for index types to avoid signed/unsigned warnings. This will fix the repeated-count output you saw.

How to correct the output, because for the bold it follows every studentid.

Why do you insist on not giving us the information we need to understand what you want? When was the last time you read the post Read Me: Read This Before Posting? Start explaining in detail what you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.