I keep getting an error "cannot convert parameter 1 from 'char (*)[15]' to 'Student_Type *" in the swap call.
gradclass is a struct and Student_Type is the name of the struct. Im trying to sort names with this. Anyone know how to fix this?

void SortNames(Student_Type* gradclass, int i)
{
   int smallest;
     for(int firstUnsorted = 0; firstUnsorted < 12 -1; firstUnsorted++)
     {
         smallest = firstUnsorted;
         for(int current = smallest + 1; current < 12; current++)
         {
                 if(*gradclass[current].lastname < *gradclass[smallest].lastname)
                       smallest=current;
         }
         Swap(&gradclass[firstUnsorted].lastname, &gradclass[smallest].lastname);
     }


}
void Swap(Student_Type* element1, Student_Type* element2)
{
  Student_Type temp=*element1;
  *element1=*element2;
  *element2=temp;
}

Recommended Answers

All 12 Replies

line 9: remove the stars. Also, I assume lastname is std::string ?? If simple character array then that comparison won't work.

line 12: should sort the entire structure, not just one of its members Swap(&gradclass[firstUnsorted], &gradclass[smallest]);

Yeah it is a simple character array, I cant figure out how to compare them. Any ideas?

call strcmp() if( strcmp(gradclass[current].lastname, gradclass[smallest].lastname) < 0)

Still getting the same error, maybe If I post the whole code that will help.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct Student_Type
{
	char lastname[15];
	char firstname[15];
	char ID[15];
	
};


void AVERAGEDISPLAY(float average, float average2);
void SortNames(Student_Type*, int);
void Swap(Student_Type* , Student_Type* );



int main()
{
	ifstream inData;
	Student_Type gradclass[10];
	int i=0, t=0;
	int size=12;
	float grade[15][8];
	float sum=0,total, total2=0;
	float average=0,average2=0;

	inData.open("TextFile1.txt");



	while (inData>>gradclass[i].lastname>>gradclass[i].firstname>>gradclass[i].ID)
	{	
		SortNames(gradclass, i);
		cout<<gradclass[i].lastname<<" "<<gradclass[i].firstname<<" "<<gradclass[i].ID<<" ";
	
		for (int j=0; j<8; j++)//loop for studentgrades
            {
				
	         inData >> grade[t][j];
	         cout<<fixed<<setprecision(1)<<grade[t][j]<<" ";
			 sum=sum+grade[t][j];
            }
		total2=sum/8;//averages individual student grade
			cout<<setw(5)<<total2;
			average+=total2;//adds for average grade
			sum=0;
			t++;
			cout<<endl;
		
	}
	cout<<endl;

	cout<<"SUBJECT AVERAGES ARE:";
		
		for (int j=0; j<8; j++)//loop for subject averages
		{	total=0;
            for (int i=0; i<12; i++)
            { 
	         inData >> grade[i][j];
			 total+=grade[i][j];//adds subjects only
            }
			cout<<fixed<<setprecision(1)<<setw(5)<<total/12<<" ";
			average2+=total/12;//adds subject averages
		}
		cout<<endl;
		AVERAGEDISPLAY(average, average2);//displays student and subject grade averages

	
return 0;

}

void AVERAGEDISPLAY(float average, float average2)
{
	cout<<endl<<endl;
	cout<<"AVERAGE SCORE USING STUDENT AVERAGES:"<<average/12<<endl;//averages student averages
	cout<<"AVERAGE SCORE USING SUBJECT AVERAGES:"<<average2/8<<endl<<endl;//averages subject averages
}
void SortNames(Student_Type* gradclass, int i)
{
   char smallest;
     for(int firstUnsorted = 0; firstUnsorted < 12 -1; firstUnsorted++)
     {
         smallest = firstUnsorted;
         for(int current = smallest + 1; current < 12; current++)
         {
                 if( strcmp(gradclass[firstUnsorted].lastname, gradclass[smallest].lastname) < 0)
                       smallest=current;
         }
         Swap(&gradclass[firstUnsorted].lastname, &gradclass[smallest].lastname);
     }


}
void Swap(Student_Type* element1, Student_Type* element2)
{
  Student_Type temp=*element1;
  *element1=*element2;
  *element2=temp;
}

line 95: see my previous post (#2) which you failed to implement.

Oh yeah ok that did work, thanks. One last thing and this might be dumb, but how do I output the data? I seem to always get weird numbers or an infinite loop.

Please identify the lines in your program that are giving you the problem(s).

Lines 75-78 I cant seem to get the output right. Its all weird numbers or infinite loop. Am I not outputing the data right...or?

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct Student_Type
{
	char lastname[15];
	char firstname[15];
	char ID[15];
	
}student;

void HEADING();
void AVERAGEDISPLAY(float average, float average2);
void SortNames(Student_Type*, int);
void Swap(Student_Type* , Student_Type* );



int main()
{
	ifstream inData;
	Student_Type gradclass[10];
	int i=0, t=0;
	int size=12;
	float grade[15][8];
	float sum=0,total, total2=0;
	float average=0,average2=0;

	inData.open("TextFile1.txt");

HEADING();

	while (inData>>gradclass[i].lastname>>gradclass[i].firstname>>gradclass[i].ID)
	{	
		
		
		cout<<gradclass[i].lastname<<" "<<gradclass[i].firstname<<" "<<gradclass[i].ID<<" ";
	
		for (int j=0; j<8; j++)//loop for studentgrades
            {
				
	         inData >> grade[t][j];
	         cout<<fixed<<setprecision(1)<<grade[t][j]<<" ";
			 sum=sum+grade[t][j];
            }
		total2=sum/8;//averages individual student grade
			cout<<setw(5)<<total2;
			average+=total2;//adds for average grade
			sum=0;
			t++;
			cout<<endl;

	
	}
	cout<<endl;

	cout<<"SUBJECT AVERAGES ARE:";
		
		for (int j=0; j<8; j++)//loop for subject averages
		{	total=0;
            for (int i=0; i<12; i++)
            { 
	         inData >> grade[i][j];
			 total+=grade[i][j];//adds subjects only
            }
			cout<<fixed<<setprecision(1)<<setw(5)<<total/12<<" ";
			average2+=total/12;//adds subject averages
		}
		cout<<endl;
		AVERAGEDISPLAY(average, average2);//displays student and subject grade averages
		
		SortNames(gradclass, i);
		for(int k=0;k<12;k++)
		cout<<gradclass[k].lastname<<endl;
		
		
return 0;

}
void HEADING ()
{
cout<<"-Student Name   ID    SUB1  SUB2  SUB3  SUB4  SUB5  SUB6  SUB7  SUB8 AVERAGE-"<<endl;
	cout<<"----------------------------------------------------------------------------";
	cout<<endl<<endl;
}
void AVERAGEDISPLAY(float average, float average2)
{
	cout<<endl<<endl;
	cout<<"AVERAGE SCORE USING STUDENT AVERAGES:"<<average/12<<endl;//averages student averages
	cout<<"AVERAGE SCORE USING SUBJECT AVERAGES:"<<average2/8<<endl<<endl;//averages subject averages
}
void SortNames(Student_Type* gradclass, int i)
{
   int smallest;
     for(int firstUnsorted = 0; firstUnsorted < 12 -1; firstUnsorted++)
     {
         smallest = firstUnsorted;
         for(int current = smallest + 1; current < 12; current++)
         {
                 
			if(*gradclass[firstUnsorted].lastname < *gradclass[smallest].lastname)
					smallest=current;
			
         }
         Swap(&gradclass[firstUnsorted], &gradclass[smallest]);
     }

}
void Swap(Student_Type* element1, Student_Type* element2)
{
  Student_Type temp=*element1;
  *element1=*element2;
  *element2=temp;
}

I also need that text file so that I can run your program and see what's happening. If its very long then just attach it to your post instead of copy/past into the post itself.

line 104: wrong. I already told you how to use strcmp() to compare character arrays.

here is the textfile...or whats in it.


Lowery, frank 1234575 78.0 55.8 60.3 39.7 53.0 64.5 57.9 49.1
Smith, Joy 1234545 57.4 88.4 54.0 97.1 49.5 78.4 91.3 39.4
Ronald, Mike 1234567 54.8 58.4 94.6 44.5 84.0 67.7 84.6 78.9
Francis, Todd 6789087 74.8 73.0 98.0 57.6 88.0 46.8 73.6 58.3
Kurosawa, Odd 5678355 65.7 78.9 45.9 65.4 33.9 74.9 47.9 78.0
Crane, Don 2345676 57.6 75.4 87.4 76.4 44.4 75.6 65.7 67.0
William, Jay 6789754 67.9 45.7 45.7 78.7 75.4 45.7 49.6 85.7
Lorenzo, Franny 1345789 84.7 74.8 44.9 75.3 84.3 23.0 87.3 43.8
Lakeshore, Sarah 6789096 89.6 74.9 78.3 78.3 37.5 82.4 79.6 98.0
Lenny, Joker 1234566 44.3 84.9 43.7 39.0 43.9 74.6 74.0 74.2
Fifa, Louis 1234545 67.8 83.7 73.0 78.3 69.7 74.5 66.9 73.9
Fooly, Cooly 7544588 78.9 33.8 47.7 65.9 23.8 78.8 48.4 45.8

Your program doesn't work because of several problems:
1) line 25: The array is too small. Your file contains 15 student records, but the array can only hold 10. Increase the size of the array to at least 15 (or the maximum in the data file). One way to resolve the problem is to use c++ vector class and you don't have to know how many student records are in the file.

2) line 104 is wrong. see previous posts

3) line 75: the value of i is wrong at that point because you previously used that variable on line 64. Declare a different variable that contains the number of records that were read in the data file and don't use it for anything else (except to pass to other functions).


4) line 95: The sort function has hard-coded the record count and ignores the integer that was pass to it. You need to correct that problem.

After correcting the above errors I get this outout:

-Student Name   ID    SUB1  SUB2  SUB3  SUB4  SUB5  SUB6  SUB7  SUB8 AVERAGE-
----------------------------------------------------------------------------

Lowery, frank 1234575 78.0 55.8 60.3 39.7 53.0 64.5 57.9 49.1  57.3
Smith, Joy 1234545 57.4 88.4 54.0 97.1 49.5 78.4 91.3 39.4  69.4
Ronald, Mike 1234567 54.8 58.4 94.6 44.5 84.0 67.7 84.6 78.9  70.9
Francis, Todd 6789087 74.8 73.0 98.0 57.6 88.0 46.8 73.6 58.3  71.3
Kurosawa, Odd 5678355 65.7 78.9 45.9 65.4 33.9 74.9 47.9 78.0  61.3
Crane, Don 2345676 57.6 75.4 87.4 76.4 44.4 75.6 65.7 67.0  68.7
William, Jay 6789754 67.9 45.7 45.7 78.7 75.4 45.7 49.6 85.7  61.8
Lorenzo, Franny 1345789 84.7 74.8 44.9 75.3 84.3 23.0 87.3 43.8  64.8
Lakeshore, Sarah 6789096 89.6 74.9 78.3 78.3 37.5 82.4 79.6 98.0  77.3
Lenny, Joker 1234566 44.3 84.9 43.7 39.0 43.9 74.6 74.0 74.2  59.8
Fifa, Louis 1234545 67.8 83.7 73.0 78.3 69.7 74.5 66.9 73.9  73.5
Fooly, Cooly 7544588 78.9 33.8 47.7 65.9 23.8 78.8 48.4 45.8  52.9

SUBJECT AVERAGES ARE: 68.5  69.0  64.5  66.3  57.3  65.6  68.9  66.0


AVERAGE SCORE USING STUDENT AVERAGES:65.8
AVERAGE SCORE USING SUBJECT AVERAGES:65.8

Lowery,
Smith,
Ronald,
Francis,
Kurosawa,
Crane,
William,
Lorenzo,
Lakeshore,
Lenny,
Fifa,
Fooly,
Press any key to continue . . .

It seems you try to catch a flea on the dead dog.

Better leave in peace this ill-fated sorting. This program can't input data file!
Start from the line 36 and totally revise literally every input statement...

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.