How to make a pair programming..

like I have 1 2 3 4 5 6 7
First, I want to make a pair of 91,20
(1,3)
(1,4)
(1,5)

using i=1, i<9,i++

I want to do like this, but how to call from i=0, i<11; i++, and how o insert in this program.

#include <iostream.h>

template<class C> 
class Pair
{
public:
	Pair(C, C);
	Pair(Pair&);
	void Display();

private:
	C first;
	C second;
};

template<class C> 
Pair<C>::Pair(C left, C right)
{
	first = left;
	second = right;
}


template<class C> 
Pair<C>::Pair(Pair<C>& p)
{
	first = p.first;
	second = p.second;
}

template<class C> 
void Pair<C>::Display()
{
	cout<<"(" << first <<","<< second <<")"<<"\n";
}

int main()
{
	Pair<int> pi(2,3);

	pi.Display();

	return 0;
}

Well since you want to do it in a for loop, you must realize that this is probably a job for pointers since you can't make a for loop generate names for your variables to hold the class. What I would do is this:

int main()
{
    const int SIZE = 8;
    Pair<int> * pairs[SIZE]; // Declare an array of pointers to Pair<int>
    for (int i = 0; i < SIZE; i++)
        pairs[i] = new Pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array

	pairs[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
	return 0;
}

I dont understand

If you do not understand what pointers are or how they work, it would not be fitting to describe every aspect of them in this thread.

I will however give you a good tutorial on pointers, and when you practice with their examples, you should have a decent grasp of them, then you may come back to the example I gave previously and it should seem more clear.

http://www.cplusplus.com/doc/tutorial/pointers.html

I understand now, but a lot of error.


error C2065: 'pairs' : undeclared identifier
error C2109: subscript requires array or pointer type
error C2061: syntax error : identifier 'Pair'
error C2109: subscript requires array or pointer type
error C2227: left of '->Display' must point to class/struct/union
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\78.cpp(22) : fatal error C1003: error count exceeds 100; stopping compilation
Error executing cl.exe.

78.exe - 102 error(s), 0 warning(s)

Can you post your source now please? (and edit out the big error list, it is mostly the same error over and over again and is just a big waste of space)

I will see what you have and try to help you out.

Here it is..

#include <iostream>
usingnamespace std;
{
    const int students.size = 12;
    Pair<int> * pairs[students.size]; // Declare an array of pointers to Pair<int>
    for (int i = 0; i < students.size; i++)
    pairs[i] = new Pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array

	pairs[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
	
	return 0;
	}

1.
Did you make sure you kept what you had in front of main? I remove that from the code i posted as it is the same as you had, only the things in main were what i changed.

2.
students.size is referring to a data member of an object that doesn't exist. Moreover, since it would be a data member inside an object, it couldn't be declared again. Use a plain variable name instead of students.size. maybe students_size

This is a full code. but error.fatal error C1003: error count exceeds 100; stopping compilation
Error executing cl.exe.

is it cannot exceed 100?

/*------------------------------------------------------------------------------------------+
|                           Filename : ConflictMatrix.cpp                                   |
+------------------------------------------------------------------------------------------*/

#include <iostream>  // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for each()
using namespace std; // import "std" namespace into global namespace


   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 <<i<<":\t" ;   // 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";       
					} 
/*--------------------------------------------------------------------------------------------+
|                         counting elements in StudentID                                     |
+--------------------------------------------------------------------------------------------*/
  
	{
    const int student_size = 12;
    Pair<int> * pairs[student_size]; // Declare an array of pointers to Pair<int>
    for (int i = 0; i < student_size; i++)
    pairs[i] = new Pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array

	pairs[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
	
	return 0;
	}
/*-------------------------------------------------------------------------------------------+		
|                                         Matrix ExamID                                       
+--------------------------------------------------------------------------------------------*/
   {
cout<<"i,j|";
for (int colHead = 1; colHead < 11; colHead++)
cout << setw(3) << colHead;
cout << endl;
cout <<"__________________________________"<<endl;

			for (int rowVal = 1; rowVal < 11; rowVal++)
		{
			cout << setw(3)<< rowVal <<'|';

			for (int colVal =1; colVal < 11; colVal++)
			cout << setw(3)<<"x";
			cout <<endl;
		}
	} 
}

What is the class of Display?


error C2061: syntax error : identifier 'pair'
error C2109: subscript requires array or pointer type
error C2227: left of '->Display' must point to class/struct/union

{
	int pair;
    const int student_size = 12;
    pair <int> * pair[student_size]; // Declare an array of pointers to Pair<int>
    for (int i = 0; i < student_size; i++);
    
	{
	pair[i] = new pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array

	pair[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
	}
	return 0;
	}

What is the class of Display?


error C2061: syntax error : identifier 'pair'
error C2109: subscript requires array or pointer type
error C2227: left of '->Display' must point to class/struct/union

{
	int pair;
    const int student_size = 12;
    pair <int> * pair[student_size]; // Declare an array of pointers to Pair<int>
    for (int i = 0; i < student_size; i++);
    
	{
	pair[i] = new pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array

	pair[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
	}
	return 0;
	}

delete line 3 because it hids the c++ class by the same name.

error C2976: 'pair' : too few template arguments
see declaration of 'pair'

Why there is too few template arguments when i delete line 3?

So, I put put like this?

{
typedef std::pair<int,int>       RandomPair;
typedef std::vector<RandomPair>  VectorRandomPair;

    const int student_size = 12;
    pair <int> * pair[student_size]; // Declare an array of pointers to Pair<int>
    for (int i = 0; i < student_size; i++)
    	
	pair[i] = new pair<int>(1,i); // Construct each new pair and give its address to an index in the pointer array

	pair[3]->Display(); //since Display is a member function of an object, and pairs[num] is a pointer to that object, you use the arrow operator to dereference and call the member function
	
	return 0;
	}

Here is a pretty good explaination of pair. As its name implies, and that link explicitly states, pair required two objects, not one as you have on line 7 And you can not have the name of the array the same as the class name -- the array name must be something else.

const int student_size = 12;
pair<int,int>* array[student_size];
 for (int i = 0; i < student_size; i++)   	
      array[i] = new pair<int,int>(1,i); // Construct each new pair and give its address to an index in the pointer array

I didn't compile or test the above, so use it at your own risk.

Thank you ancient dragon. It succefully work out!!!!

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.