How to cal the examid like i want to call 001 and count how many exam 001 clash with 002, 003, 004...?

// Filename : ConflictMatrix.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

struct student
{
     string studentid;
     vector <int> examcode;	
};

 
int main()

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

    return 0; 
}

Recommended Answers

All 12 Replies

It's a little easier to read when you put it in C++ format with C++ code tags since a lot of lines overflow.

Put "code=C++" in the first bracket rather than just "code" and it gives the line numbers and makes the comments green.

I'm not sure what you are asking. You want to count the number of unique exam codes? What do you mean by "clash"? Can you be more specific?

// Filename : ConflictMatrix.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

struct student
{
     string studentid;
     vector <int> examcode;	
};

 
int main()

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

    return 0; 
}

I'm not sure what you are asking. You want to count the number of unique exam codes? What do you mean by "clash"? Can you be more specific?

Dear master Vernon,

After do the read data from file, i want to call the examid, from 001 untul finish which was 0139.

for 001 i want to count how many students take 001 and 002, 001 and 003, 001 and 004....001 and 0139.

it will be a two multidimensional array i think.
.....001..002...003..004...005..006
001
002
003
004
005


i'm soryy because i dont put a "code=C++" , because I DONT KNOW about it. hehehe . sorry again.

I think a two dimensional array would be a good move. You do not know ahead of time, I imagine, the number of different exams. So you will not know the dimensions of the array ahead of time. So you'll have to dynamically declare a 2-dimensional array. Before doing so you will need to be able to go through the big vector of type student and count the number of unique exam codes, as well as extract those unique exam codes. Your 2-dimensional array's dimensions will be determined by that count.

To call the exam id 001..002..003 is like below?

"code=C++"

 for (int j = 0; j < students.at (i).examcode.size (); j++)
    cout << students.at (i).examcode.at (j) << "\t";
//This to produce examid only...//
 for (int i = 0; i < students.size (); i++)
for (int j = 0; j < students.at (i).examcode.size (); j++)	
 { 	cout << students.at (i).examcode.at(j)<<endl;   // output list of exam codes for this student
	 }      
}

how to make the exam id in increasing number?

How to call the exam id only?
is it like this?
cout << students.at(i).examcode.at(j)<<endl;

I assume you mean "access", not "call". There is no "exam id" in your struct. There is "studentid" and "examcode".

You need to be more careful and detailed when writing your posts, and you need to preview your posts before you submit them. I put "code=C++" in quotes because when I put them in brackets, the web page didn't recognize that I wanted to display the actual words. I see you just posted with the C++ code tags so that's good, but you need to indent so people can follow the logic. That's the whole point of the code tags. The code tags are pointless when the spacing is so strange.

Reread my previous post. Doing what you want to do is going to require much more than the few lines that you've posted. Describe what you have done, what you know how to do, what you don't, what's worked, what's failed, etc., and ask a specific question. This will take more than a few lines of code and more than one sentence to describe.

You need to be more careful and detailed when writing your posts, and you need to preview your posts before you submit them. I put "code=C++" in quotes because when I put them in brackets, the web page didn't recognize that I wanted to display the actual words. I see you just posted with the C++ code tags so that's good, but you need to indent so people can follow the logic. That's the whole point of the code tags. The code tags are pointless when the spacing is so strange.

In other words
Read the Rules -- as you were asked when you registered.
Read This because it's title says you should
and Read This to understand proper formatting.

Yes, there's some reading involved, but you want us to be able to help you -- right? The more we can understand, the better the help.

oic..examid is examcode..so i wrong..to access the exam id in increasing order how?

//This to produce examcode only...//
 for (int i = 0; i < students.size (); i++)
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
	 }
#include <algorithm>

// for each student
for(int i = 0; i < students.size(); i++)
   {
   // display exam codes in ascending order
   std::sort( students.at(i).examcode.begin(), students.at(i).examcode.end() );

   for (int j = 0; j < students.at(i).examcode.size (); j++)
      cout << students.at (i).examcode.at(j) << "\n";
   }

see reference for sort() here: http://www.cppreference.com/cppalgorithm/sort.html

oic..examid is examcode..so i wrong..to access the exam id in increasing order how?

Did you read any of WaltP's links? Did you read my post? You AGAIN ask how to access a non-existent variable called "exam id", when earlier in the sentence you show that you realize that it does not exist. After the links were posted explaining how to indent code for higher readability, you again post code like this:

//This to produce examcode only...//
 for (int i = 0; i < students.size (); i++)
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
	 }

Compare the above code to this code below:

//This to produce examcode only...//
for (int i = 0; i < students.size (); i++)
{
    for (int j = 0; j < students.at (i).examcode.size (); j++)	
    {
	cout << students.at (i).examcode.at(j)<<"\t";
    }
}

One is easily readable, one is not. If you go over posts from your earlier threads, there is an awful lot of posts where the problem is that you put code in the wrong place and neither you nor people trying to help were able to spot it. I think that is in large part because the spacing you use makes it extremely hard to spot stuff like missing brackets. Please read the three links that WaltP posted and follow their guidelines. You are far more likely to get good help if you do.

I want to do a 2 dimensional array, but how to put automatically in colum and row the examcode?
below, i must put one by one..

/*-------------------
Two dimensional array
-------------------*/

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>


ifstream infile("STA83EXM.txt");


void main()
{


	const int row = 139;
	const int column = 139;
	int matrix[row] [column];

/*---------------
creates Matrix
---------------*/
	int num;
		for(int i = 0; i<=row-1; i++)
		{
			for(int j = 0; j<=column-1; j++)
			{
				infile>>num;
				matrix[i][j]=num;
			}
		}
	

/*----------------
Prints out Matrix
-----------------*/


	cout<<"\n\n"<<endl;
	cout<<"\t\tColumn:"<<setw(3)<<"1"<<setw(5)<<"2"<<setw(5)<<"3"<<setw(5)<<"4"<<setw(5)<<"5"<<setw(5)<<"6"<<endl;
	cout<<"\t\t--------------------------------------"<<endl;
	cout<<"\t\tRow 1|\t "<<setw(2)<<matrix[0][0]<<setw(5)<<matrix[0][1]<<setw(5)<<matrix[0][2]<<setw(5)<<
		matrix[0][3]<<setw(5)<<matrix[0][4]<<setw(5)<<matrix[0][5]<<endl;
	cout<<"\t\t    2|\t "<<matrix[1][0]<<setw(5)<<matrix[1][1]<<setw(5)<<matrix[1][2]<<setw(5)<<
		matrix[1][3]<<setw(5)<<matrix[1][4]<<setw(5)<<matrix[1][5]<<endl;
	cout<<"\t\t    3|\t "<<matrix[2][0]<<setw(5)<<matrix[2][1]<<setw(5)<<matrix[2][2]<<setw(5)<<
		matrix[2][3]<<setw(5)<<matrix[2][4]<<setw(5)<<matrix[2][5]<<endl<<endl;
	


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