Can anybody help me whis this error? in red colour...

error C2109: subscript requires array or pointer type
error C2109: subscript requires array or pointer type
: error C2109: subscript requires array or pointer type
error C2664: 'void __cdecl std::swap(int &,int &)' : cannot convert parameter 1 from 'int' to 'int &'
A reference that is not to 'const' cannot be bound to

#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 exam
{
int examid;
vector <int> total;
};

void SwapMembers (int items[], int index1, int index2)
{
	int temp;
	temp=items[index1];
	items[index1]=items[index2];
	items[index2]=temp;
}

	int main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
   int tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // 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.
{
	ofstream myfile;
	myfile.open("411.txt");
	int temp;
	if (myfile.is_open())
	{
		for (size_t i = 0; i < exams.size(); i++) 
		{
		for (size_t j = 0; j<exams.at(i).total.size(); j++) 
		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
		}		
	}	
					
cin.get();
return 0;

for (size_t i = 0; i < exams.size(); i++) 
for (size_t j = 0; j<exams.at(i).total.size(); j++) 

{

int min = i;
for (size_t j = 0; j<exams.at(i).total.size(); j++) 
[B]if(exams.at (i).total.at(j)[min]<exams.at (i).total.at(j)[j])[/B]
min = j;
swap(exams.at (i).total.at(j)[min],exams.at (i).total.at(j));
}
for (size_t j = 0; j<exams.at(i).total.size(); j++) 
cout << exams.at (i).total.at(j)<<" "<< endl;
}
}

Recommended Answers

All 13 Replies

There are some missing '{' brackets after line 69,75,80. Can you try after fixing them. and if that's just a typo then plz paste the corrected version.

I have done a correction what you had told but it seems still error again..
error C2143: syntax error : missing ')' before '}'
67) : error C2780: 'void __cdecl std::swap(_Ty &,_Ty &)' : expects 2 arguments - 0 provided
see declaration of 'swap'
syntax error : missing ';' before '}'
: fatal error C1004: unexpected end of file found
412.cpp
: error C2109: subscript requires array or pointer type
subscript requires array or pointer type
subscript requires array or pointer type
: error C2664: 'void __cdecl std::swap(int &,int &)' : cannot convert parameter 1 from 'int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(82) : error C2374: 'j' : redefinition; multiple initialization
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(76) : see declaration of 'j'
Error executing cl.exe.

#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 exam
{
int examid;
vector <int> total;
};

void SwapMembers (int items[], int index1, int index2)
{
	int temp;
	temp=items[index1];
	items[index1]=items[index2];
	items[index2]=temp;
}

	int main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
   int tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // 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.
{
	ofstream myfile;
	myfile.open("411.txt");
	int temp;
	if (myfile.is_open())
	{
		for (size_t i = 0; i < exams.size(); i++) 
		{
		for (size_t j = 0; j<exams.at(i).total.size(); j++) 
		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
		}		
	}	
					
cin.get();
return 0;

for (size_t i = 0; i < exams.size(); i++) 
{
for (size_t j = 0; j<exams.at(i).total.size(); j++) 

{

int min = i;
for (size_t j = 0; j<exams.at(i).total.size(); j++) 
{
if(exams.at (i).total.at(j)[min]<exams.at (i).total.at(j)[j])
min = j;
swap(exams.at (i).total.at(j)[min],exams.at (i).total.at(j));
}
for (size_t j = 0; j<exams.at(i).total.size(); j++) 
{
cout << exams.at (i).total.at(j)<<" "<< endl;
}
}
}
}
	}

swap(exams.at (i).total.at(j)[min],exams.at (i).total.at(j)); What's the [min] doing?

min means to get the minimum

you set min to be the value of j, so that statement is equivalent to exams.at (i).total[j][j] , but .total is not a two dimensional array, is it? That's what the error C2109 is about, you're putting an excess array index on this expression.

I want to sort the number in descending order...but how to declare a exams.at (i).total.at(j)" in if statement?

#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 exam
{
int examid;
vector <int> total;
};

void SwapMembers (int items[], int index1, int index2)
{
	int temp;
	temp=items[index1];
	items[index1]=items[index2];
	items[index2]=temp;
}

	int main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
   int tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // 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.
{
	ofstream myfile;
	myfile.open("411.txt");
	
	if (myfile.is_open())
	{
		for (size_t i = 0; i < exams.size(); i++) 
		{
		for (size_t j = 0; j<exams.at(i).total.size(); j++) 
		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
	    if( j< j-1)
        swap(j,j+1);
		cout<<" "<< exams.at (i).total.at(j);
		}
	}
					
cin.get();
return 0;
}
}

I want to sort the number in descending order...but how to declare a exams.at (i).total.at(j)" in if statement?

> you mean

if((exams.at (i).total.at(j)) < somthing)

yess.....the outpu is like this...

1: 25
2: 20
3: 46
4: 56
5: 12
6: 22

and i want to sort in descending order
4: 56
3: 46
1: 25

you can use any of the sorting algorithms. Try bubble sort, it's one of the easiest and since the number of elements is not too large you dont need to worry about performance.

before that you need to fix some other errors in your code too. within nested for loops that counter you use should have different names. all your nested loops have 'j'.

but how to make an if statement?

j<j+1 or j+1>j?

if( j < (j+1) )
{
//code
}

I have tried to do a bubble sort..but error..what is 'size' : function does not take 1 parameters


error C2660: 'size' : function does not take 1 parameters
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(64) : error C2660: 'size' : function does not take 1 parameters
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(66) : error C2660: 'size' : function does not take 1 parameters
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(67) : error C2660: 'size' : function does not take 1 parameters
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(67) : error C2660: 'size' : function does not take 1 parameters
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(68) : error C2660: 'size' : function does not take 1 parameters
Error executing cl.exe.

412.exe - 6 error(s), 0 warning(s)

#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 exam
{
int examid;
vector <int> total;
};

void SwapMembers (int items[], int index1, int index2)
{
	int temp;
	temp=items[index1];
	items[index1]=items[index2];
	items[index2]=temp;
}

	int main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
   int tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // 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.
{
	ofstream myfile;
	myfile.open("411.txt");
	
	int temp, flag;
	if (myfile.is_open())
	{
		for (size_t i = 0; i < exams.size(); i++) 
		{
			for (size_t j = 0; j<exams.at(i).total.size(); j++) 
			{
				if (exams.at(i).total.size(j+1) > exams.at(i).total.size(j))      // ascending order simply changes to <
				{ 
                    temp = exams.at(i).total.size(j);             // swap elements
                    exams.at(i).total.size(j) = exams.at(i).total.size(j+1);
                    exams.at(i).total.size(j+1) = temp;
                    flag = 1;               // indicates that a swap occurred.
                }

		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
	  	cout<<" "<< exams.at (i).total.at(j)<<"\t";
			}
		}
	}
					
cin.get();
return 0;
}
}

you dont need to use 'size' in the if statement, you can compare the values itself, using the 'at' function was enough.

compare the values "exams.at(i).total.at()"

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.