How to avoid the output for
10 appears 3 times.
20 appears 3 times.
repeat at each line. Im just want one only.


10 appears 3 times.
20 appears 3 times. s610
6 29 69 74 95 103 110 132

10 appears 3 times.
20 appears 3 times. s611
2 7 29 70 75 100 104 111

10 appears 3 times.
20 appears 3 times. Press any key to continue

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
------------------------*/
      
	  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";

Could you post this code in a bit more context? Also post the source code of count() .

/*------------------------------
Programming to count the examids
------------------------------*/

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include<iomanip>
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
------------------------*/
	 }      
	  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";

/*-----------------------------
 counting elements in container
-----------------------------*/

	  vector<int> myvector (myints, myints+8);
      mycount = (int) count (myvector.begin(), myvector.end(), 20);
      cout << "20 appears " << mycount  << " times.\t";
	}	
}
      return 0;
}
}
	 }

If you're asking why you get

10 appears 3 times.
20 appears 3 times.

each time, it's because you coded it that way.

int myints[] = {10,20,30,30,20,10,10,20};
mycount = (int) count (myints, myints+8, 10);
cout << "10 appears " << mycount << " times.\n"; // this is the first line of output

vector<int> myvector (myints, myints+8);
mycount = (int) count (myvector.begin(), myvector.end(), 20);
cout << "20 appears " << mycount  << " times.\t"; // this is the second line

How to make a correction, because I just want one line only.

>How to make a correction, because I just want one line only.
Remove one of the cout statements.

i need both of cout, but dont want it repeat each time of student...........

Just remove the '\n' in the cout string: cout << "10 appears " << mycount << " times.\n" =becomes=> cout << "10 appears " << mycount << " times." new output: 10 appears 3 times. 20 appears 3 times.

cannot remove tne /n, because it will appear many times again

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.