here is the question:
Write a program that reads names and gpa’s from a text file. The file looks like:

James 3.9
Margaret 3.5
Charles 1.2
Jennifer 4.0

Your program sorts the students ascending by gpa, and prints the sorted list including names. To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers. Sort the numbers. Whenever you swap 2 numbers also swap their matching names in the names array. Include a structure chart as in the model lab.

Here is the error i keep getting:

Error 1 error C2664: 'gpa_Sorter' : cannot convert parameter 1 from 'double [2000]' to 'double' c:\users\goncalvesa2\documents\visual studio 2010\projects\lab2problem2\lab2problem2\lab2problem2.cpp 31 1 Lab2Problem2


Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 2000

using namespace std;

ifstream in;
int readArray(string* a);
void gpa_Sorter(double studentGPA, int size);

int main()
{
	string a[MAX];
	double studentGPA[MAX];

	in.open("TextFile1.txt");
	if(!in.is_open ())
	{
		cout << "Failed to open "<< endl;
		cout << "Now exiting program...\n";
	}

	if (in.fail())
	{
		cout << "opening file failed." << endl;
	}

	int size = readArray(a);
	gpa_Sorter(studentGPA, size);

	for (int i = 0; i < size; i++)
	{
		cout << a[i] << " ";
	}

return 0;
}

int readArray(string a)
{

	double studentGPA[MAX];
	int i = 0;

	while(i < MAX && !in.eof())
	{
		in >> a[i];
		in >> studentGPA[i];
		i++;
	}
	if (i > MAX)
	{
		cout << "the file truncated at " << MAX << endl;
	}
	return i;
}

void gpa_Sorter(double studentGPA[], int size)
{
	for (int pass = 0; pass < size - 1; pass++)
	{
		for( int cell = 0; cell < size - pass - 1; cell++)
		{
			if(studentGPA[cell] > studentGPA[cell] + 1)
			{
				double temp = studentGPA[cell];
				studentGPA[cell] = studentGPA[cell + 1];
				studentGPA[cell + 1] = temp;
			}
		}
	}

}

Recommended Answers

All 13 Replies

In line 11 you say that gpa_Sorter takes two arguments: a double and an int. In line 31 you call gpa_Sorter with an array of double. Thus the error message.

so then what should i do to change that?
change line 11 to both doubles, and on line 31 change the array double to what?

In line 11, change "double studentGPA" to "double studentGPA[]" to match line 60.

Also, I'm afraid I need to point out that if you really do not know how to answer this question by yourself, you have a lot of studying to do.

so i changed what you said to change it i realize how stupid i am for not noticing that before but here is the new error i get:


Error 1 error LNK2001: unresolved external symbol "int __cdecl readArray(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?readArray@@YAHPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) C:\Users\Goncalvesa2\documents\visual studio 2010\Projects\Lab2Problem2\Lab2Problem2\Lab2Problem2.obj Lab2Problem2

heres my updated code:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 2000

using namespace std;

ifstream in;
int readArray(string* a);

void gpa_Sorter(double studentGPA[], int size);

int main()
{
	string a[MAX];
	double studentGPA[MAX];
	in.open("TextFile1.txt");
	if(!in.is_open ())
	{
		cout << "Failed to open "<< endl;
		cout << "Now exiting program...\n";
	}

	if (in.fail())
	{
		cout << "opening file failed." << endl;
	}

	int size = readArray(a);
	gpa_Sorter(studentGPA, size);

	for (int i = 0; i < size; i++)
	{
		cout << a[i] << " ";
	}

return 0;
}

int readArray(string a)
{

	double studentGPA[MAX];
	int i = 0;

	while(i < MAX && !in.eof())
	{
		in >> a[i];
		in >> studentGPA[i];
		i++;
	}
	if (i > MAX)
	{
		cout << "the file truncated at " << MAX << endl;
	}
	return i;
}

void gpa_Sorter(double studentGPA[], int size)
{
	for (int pass = 0; pass < size - 1; pass++)
	{
		for( int cell = 0; cell < size - pass - 1; cell++)
		{
			if(studentGPA[cell] > studentGPA[cell] + 1)
			{
				double temp = studentGPA[cell];
				studentGPA[cell] = studentGPA[cell + 1];
				studentGPA[cell + 1] = temp;
			}
		}
	}

}

It's the same problem in slightly different clothing. In line 10, you say that readArray accepts a pointer to a string; in line 41 you say that it accepts a string.

Alright arkoenig i have been fixing it up alittle bit and heres what i have now, it actually opens the command prompt and runs but spams alot of gibberish numbers. ill show you code and output file.

/*
Lab2Problem2.cpp

Write a program that reads names and gpa's from a text file (TextFile1.txt).

The program will:
- sort the students from lowest gpa to highest.
- print the sorted list, including names.

Use 2 parallel arrays, one for the gpa, the other for the students.

Sort the numbers: Whenever you swap 2 gpa's, also swap the students array.

*/

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 2000

using namespace std;

ifstream in;
int readArray(string* student, double* studentGPA);
void gpa_Sorter(string student[], double studentGPA[], int size);

int main(){
string student[MAX];
double studentGPA[MAX];

in.open("TextFile1.txt");
if(!in.is_open ()){
cout << "Failed to open "<< endl;
cout << "Now exiting program...\n";
}

if (in.fail()){
cout << "failed" << endl;
}

int size = readArray(student, studentGPA);
gpa_Sorter(student, studentGPA, size);

in.close();

return 0;
}

int readArray(string* student, double* studentGPA){

int i = 0;

while(i < MAX && !in.eof()){
in >> student[i];
in >> studentGPA[i];
i++;
}
if (i > MAX){
cout << "the file truncated at " << MAX << endl;

}
return i;
}

void gpa_Sorter(string student[], double studentGPA[], int size){

for (int pass = 0; pass < size - 1; pass++){

for( int cell = 0; cell < size - 1 - pass; cell++){
if(studentGPA[cell] > studentGPA[cell + 1]){

double temp = studentGPA[cell];
studentGPA[cell] = studentGPA[cell + 1];
studentGPA[cell + 1] = temp;

string temp2 = student[cell];
student[cell] = student[cell + 1];
student[cell + 1] = temp2;

}
}
}
for (int i = 0; i < size; i++){
cout << student[i] << "\t\t" << studentGPA[i] << "\n";
}
}

heres output:
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
Press any key to continue . . .

i feel like i have an infinite loop somewhere but cant locate it.

Your loop in lines 54-57 is wrong, because in.eof() will be true only after you have reached the end of the file. So the last time through the loop, in.eof() will be false, but the two read attempts will fail. One workable way to write such a loop is:

while (i < MAX && (in >> student[i] >> studentGPA[i]))
    ++i;

This strategy increments i only after both read attempts have succeeded.

As for your bogus output, why don't you start by printing your data before you try to sort it, so that you can see whether you read it correctly? If that works, you know to look for trouble in your sorting code; if it doesn't, you know to look elsewhere. Either way, you have made your problem easier to solve.

so i changed that and it got rid of my bogus output which is great cause that was a headache, now all i need to know is why its not opening file the output i get in the command prompt is as follows:

Failed to open
Now exiting program...
failed
Press any key to continue . . .

So i feel like either something in the text file its trying to open is unreadable, and or i am opening my file wrong. heres updated code and the text file it is reading from.

/*
Lab2Problem2.cpp

Write a program that reads names and gpa's from a text file (TextFile1.txt).

The program will:
- sort the students from lowest gpa to highest.
- print the sorted list, including names.

Use 2 parallel arrays, one for the gpa, the other for the students.

Sort the numbers: Whenever you swap 2 gpa's, also swap the students array.

*/

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#define MAX 2000

using namespace std;

ifstream in;
int readArray(string* name, double* studentGPA);
void gpa_Sorter(string name[], double studentGPA[], int size);

int main()
{
	string name[MAX];
	double studentGPA[MAX];

	in.open("C:\201\GPAfile.txt");
	if(!in.is_open ())
	{
		cout << "Failed to open "<< endl;
		cout << "Now exiting program...\n";
	}

if (in.fail())
{
	cout << "failed" << endl;
}

int size = readArray(name, studentGPA);
gpa_Sorter(name, studentGPA, size);

in.close();

return 0;
}

int readArray(string* name, double* studentGPA)	
{

	int i = 0;

// while(i < MAX && !in.eof())	
while (i < MAX && (in >> name[i] >> studentGPA[i]))
{
	in >> name[i];
	in >> studentGPA[i];
	i++;
}
	if (i > MAX)	
	{
		cout << "the file truncated at " << MAX << endl;
	
	}
return i;
}

void gpa_Sorter(string name[], double studentGPA[], int size)
{

	for (int pass = 0; pass < size - 1; pass++)
	{

	for( int cell = 0; cell < size - 1 - pass; cell++)
		{
		if(studentGPA[cell] > studentGPA[cell + 1])
			{

				double temp = studentGPA[cell];
				studentGPA[cell] = studentGPA[cell + 1];
				studentGPA[cell + 1] = temp;
				string temp2 = name[cell];
				name[cell] = name[cell + 1];
				name[cell + 1] = temp2;

			}
		}
	}
	for (int i = 0; i < size; i++)	
		{
			cout << name[i] << "\t\t" << studentGPA[i] << "\n";
		}
}

TEXT FILE:
James 3.9
Margaret 3.5
Charles 1.2
Jennifer 4.0

When you write a string literal that contains backslash (\) characters, you need to escape each one. So if you want to read a file named C:\201\GPAFile.txt, you need to give an argument of "C:\\201\\GPAfile.txt"

Alright i made the exit for each one and now it prints 2 out 4 people people in the file:

Margaret 3.5
Jennifer 4
Press any key to continue . . .

I made a detailed suggestion as to what you should do about your input loop. Lines 59 through 64 of the program you posted are quite different from what I suggested.

That difference is the reason your program does not work. I think you should take the time to look at your code carefully. Trace through what it does by hand, including the values of all of the relevant variables, until you understand why it does not work.

Until you take the trouble to understand what you are doing and why, nothing that anyone else says will help you.

I got it working now thank you!

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.