I need help, this is confusing

I am in my second semester of a computer science class. I'm currently in Chapter 9 of the C++ From Control Structures through Objects. I'm working on program challenge #4 right now. It states: Allow the user to enter name-score pairs. For each student taking a test, the user types the student's name followed by the student's integer test score. Create a sorting function so it takes an array holding the student names and an array holding the test scores. When the sorted list of scores is displayed, each student's name should be displayed along with his or her score. Use pointers rather than array subscripts.

I have the general idea, but on the sortData I'm having trouble. I do not entirely understand how to sort the scores then display with the student names.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>

using namespace std;

int* getNumber(int&); //Allows user to input, returns the pointer to data array with reference to size
string* getName(string&); //Allows user to input, returns the pointer to data array with rederence to name
void sortData(int*, int, string*); //Sorts the input data into ascending order
void displayData(int*, int, string*); //Displays sorted data and average grade to nearest hundredth

int main()
{
int size;
string name;

int* iptr = getNumber(size); //Allows user to allocate memory and interactively enter names
string* namePtr = getName(name); //Allows user to allocate memory and interactively enter names

sortData(iptr, size, namePtr);

displayData(iptr, size, namePtr);

delete[] iptr; //Deallocate dynamic memory
delete[] namePtr; //Deallocate dynamic memory
cout << endl;
cout << " Type any key to continue--> ";
_getch();

return 0;
}
//Define getNumber() function
int* getNumber(int& size)
{
do
{
cout << " How many text scores (positive whole numbers only): ";
cin >> size;
} 
while (size <= 0);

int* iptr = new int[size]; //Allocate a block of memory
//Data entry loop
for (int i = 0; i < size; i++)
{
do
{
cout << " Enter a valid test score: ";
cin >> *(iptr + i);
}
while (*(iptr + i) < 0);
}

return iptr; //Return pointer to allocated memory
}

//Define getData() function
string* getName(string* name)

for (int i = 0; i < size; i++)
{
do
{
cout << " Enter a student's name: ";
cin >> *namePtr;
} 
while (*(namePtr + i) < 0);
}
return namePtr; //Return pointer to allocated memory
}

//Define displayData() function
void displayData(int* iptr, int size, string* namePtr)
{
cout << " Test scores: ";
for (int i = 0; i < size; i++)
{
cout << *(iptr + i) << " ";
}
cout << " Names of Students: ";
for (int i = 0; i < size; i++)
{
cout << *(namePtr + i) << " ";
}
}

//Define sortData() function
void sortData(int* iptr, int size, string* namePtr)
{
//This function sorts the data set using the selection sort algorithm
int bottom = 0, top = size - 1, min, temp, minPosition;

while (bottom < top)
{
min = *(iptr + bottom); //The initial minimum value is at the bottom of the array
minPosition = bottom; //The initial position in array of minimum value

for (int i = bottom + 1; i <= top; i++) //This loop determines the minimum value in list from bottom to top and its position in the list
{
if (*(iptr + i) < min)
{
min = *(iptr + i);
minPosition = i;
}
}
//Swap bottom with new minimum value
temp = *(iptr + bottom);
*(iptr + bottom) = min;
*(iptr + minPosition) = temp;

bottom++; //Increment bottom to new selection of array
}
}
Last edited on
closed account (D80DSL3A)
One way to solve this is to swap the names when you are swapping scores in the sortData() function.

Please use code tags. ie. place your code between the [ code] [\ code] tags provided when a new thread is created, or use the <> button below the editor window beside Format:
This improves readability and allows respondents to refer to line #'s in your code.

More about your problem.
In your sortData() function you have:
1
2
3
4
//Swap bottom with new minimum value
temp = *(iptr + bottom);
*(iptr + bottom) = min;
*(iptr + minPosition) = temp;

(I used code tags there).
Also swap the names:
1
2
3
string nameTemp = *(namePtr + bottom);
*(namePtr + bottom) = *(namePtr + minPosition);
*(namePtr + minPosition) = nameTemp;

Unfortunately, your getName() function is a bit quite messed up. You haven't allocated any memory to...where's the pointer from?
Were you given the function prototype for getName() ?
I don't see how it can work.
Consider making it string* getNames(int size); pass the size obtained from the call to getNumber(). Input the names similarly, but omit (or rework) the do{}while(); validation for the names.

Hope this helps.
Last edited on
Thanks for the advice! I haven't programmed in C++ sense my last semester, last fall. I am in my spring semester and I am very rusty on it. I'm trying to get the solution to be sorted numbers with names still with the sorted numbers. I do apologize for my awful programming, I am trying to get better. Here is a somewhat edited code, also on my visual basic it is not identifying #include<string> or any string commands.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>

using namespace std;

int* getNumber(int&); //Allows user to input, returns the pointer to data array with reference to size
string* getName(int&); //Allows user to input, returns the pointer to data array with rederence to name
void sortData(int*, int, string*);  //Sorts the input data into ascending order
void displayData(int*, int, string*);  //Displays sorted data and average grade to nearest hundredth

int main()
{
	int size;

	int* iptr = getNumber(size); //Allows user to allocate memory and interactively enter names
	string* namePtr = getName(size); //Allows user to allocate memory and interactively enter names

	sortData(iptr, size, namePtr);

	displayData(iptr, size, namePtr);

	delete[] iptr; //Deallocate dynamic memory
	delete[] namePtr;  //Deallocate dynamic memory
	cout << endl;
	cout << " Type any key to continue--> ";
	_getch();

	return 0;
}
//Define getNumber() function
int* getNumber(int& size)
{
	do
	{
		cout << " How many text scores (positive whole numbers only): ";
		cin >> size;
	} 
	while (size <= 0);

	int* iptr = new int[size]; //Allocate a block of memory
	//Data entry loop
	for (int i = 0; i < size; i++)
	{
		do
		{
			cout << " Enter a valid test score: ";
			cin >> *(iptr + i);
		}
		while (*(iptr + i) < 0);
	}

	return iptr; //Return pointer to allocated memory
}

//Define getData() function
string* getName(int& size)

string* namePtr = new int[size]; //Allocate a block of memory

//Data entry loop
for (int i = 0; i < size; i++)
{
	do
	{
		cout << " Enter a student's name: ";
		cin >> *(namePtr + i);
	}
	while (i < size);
}

return namePtr; //Return pointer to allocated memory

//Define displayData() function
void displayData(int* iptr, int size, string* namePtr)
{
	cout << " Test scores:  ";
	for (int i = 0; i < size; i++)
	{
		cout << *(iptr + i) << " ";
	}
	cout << " Names of Students:  ";
	for (int i = 0; i < size; i++)
	{
		cout << *(namePtr + i) << " ";
	}
}

//Define sortData() function
void sortData(int* iptr, int size, string* namePtr)
{
	//This function sorts the data set using the selection sort algorithm
	int bottom = 0, top = size - 1, min, temp, minPosition;

	while (bottom < top)
	{
		min = *(iptr + bottom);  //The initial minimum value is at the bottom of the array
		minPosition = bottom;  //The initial position in array of minimum value

		for (int i = bottom + 1; i <= top; i++)  /*This loop determines the minimum
                                  value in list from bottom to top and its position in the list*/
		{
			if (*(iptr + i) < min)
			{
				min = *(iptr + i);
				minPosition = i;
			}
		}
		//Swap bottom with new minimum value
		string nameTemp = *(namePtr + bottom);
		*(namePtr + bottom) = *(namePtr + minPosition);
		*(namePtr + minPosition) = nameTemp;

		bottom++;  //Increment bottom to new selection of array
	}
}
Last edited on
closed account (D80DSL3A)
That's better. Still some problems though.
In your sortData function. You want to swap both the scores and the names, so include code for both:
1
2
3
4
5
6
7
8
// swap the scores
temp = *(iptr + bottom);
*(iptr + bottom) = min;
*(iptr + minPosition) = temp;
// AND swap the names
string nameTemp = *(namePtr + bottom);
*(namePtr + bottom) = *(namePtr + minPosition);
*(namePtr + minPosition) = nameTemp;


You have an infinite loop in your getName function:
1
2
3
4
5
6
7
8
9
10
//Data entry loop
for (int i = 0; i < size; i++)
{
	do
	{
		cout << " Enter a student's name: ";
		cin >> *(namePtr + i);
	}
	while (i < size);// i never changes value in this do-while loop, so it will never complete.
}

I think there's no need for the do-while, just the for loop. Suggest trying just:
1
2
3
4
5
6
//Data entry loop
for (int i = 0; i < size; i++)
{
	cout << " Enter a student's name: ";
	cin >> *(namePtr + i);
}

Otherwise, the function looks good now. One fine point. Consider passing size by value, not by reference. The only reason to pass a small data item (like an int) by reference is if you intend for the function to change its value, which you definitely don't want in this case (though there's no code in your function which does so).

EDIT: There may be other issues in your program. I haven't looked all of it over. Further issues can be addressed after the data input functions are working properly.
Last edited on
I have the code edited but my #include<string> is not letting me use strings in the code. Such as it is not identifying my strings. So i can not test it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>

using namespace std;

int* getNumber(int);  //Allows user to input, returns the pointer to data array with reference to size
string* getName(int);  //Allows user to input, returns the pointer to data array with rederence to name
void sortData(int*, int, string*);  //Sorts the input data into ascending order
void displayData(int*, int, string*);  //Displays sorted data and average grade to nearest hundredth

int main()
{
	int size;

	int* iptr = getNumber(size);  //Allows user to allocate memory and interactively enter names
	string* namePtr = getName(size);  //Allows user to allocate memory and interactively enter names

	sortData(iptr, size, namePtr);

	displayData(iptr, size, namePtr);

	delete[] iptr; //Deallocate dynamic memory
	delete[] namePtr;  //Deallocate dynamic memory
	cout << endl;
	cout << " Type any key to continue--> ";
	_getch();

	return 0;
}
//Define getNumber() function
int* getNumber(int size)
{
	do
	{
		cout << " How many text scores (positive whole numbers only): ";
		cin >> size;
	} 
	while (size <= 0);

	int* iptr = new int[size]; //Allocate a block of memory
	//Data entry loop
	for (int i = 0; i < size; i++)
	{
		do
		{
			cout << " Enter a valid test score: ";
			cin >> *(iptr + i);
		}
		while (*(iptr + i) < 0);
	}

	return iptr; //Return pointer to allocated memory
}

//Define getData() function
string* getName(int size)

string* namePtr = new int[size]; //Allocate a block of memory

//Data entry loop
for (int i = 0; i < size; i++)
{
	cout << " Enter a student's name: ";
	cin >> *(namePtr + i);
}

return namePtr; //Return pointer to allocated memory

//Define displayData() function
void displayData(int* iptr, int size, string* namePtr)
{
	cout << " Test scores:  ";
	for (int i = 0; i < size; i++)
	{
		cout << *(iptr + i) << " ";
	}
	cout << " Names of Students:  ";
	for (int i = 0; i < size; i++)
	{
		cout << *(namePtr + i) << " ";
	}
}

//Define sortData() function
void sortData(int* iptr, int size, string* namePtr)
{
	//This function sorts the data set using the selection sort algorithm
	int bottom = 0, top = size - 1, min, temp, minPosition;

	while (bottom < top)
	{
		min = *(iptr + bottom);  //The initial minimum value is at the bottom of the array
		minPosition = bottom;  //The initial position in array of minimum value

	//This loop determines the minimum value in list from bottom to top and its position in the list
		for (int i = bottom + 1; i <= top; i++)  
		{
			if (*(iptr + i) < min)
			{
				min = *(iptr + i);
				minPosition = i;
			}
		}
		// swap the scores
		temp = *(iptr + bottom);
		*(iptr + bottom) = min;
		*(iptr + minPosition) = temp;
		// AND swap the names
		string nameTemp = *(namePtr + bottom);
		*(namePtr + bottom) = *(namePtr + minPosition);
		*(namePtr + minPosition) = nameTemp;

		bottom++;  //Increment bottom to new selection of array
	}
}
closed account (D80DSL3A)
That seems like an odd error to suddenly pop up.
Please include actual error messages when you encounter them.
I see a problem which may be causing this. You somehow nuked the braces {} wrapping the getName() function!
I fixed the braces{} issue, they most of been deleted when I went back to edit it. However the string syntax is working, just the "string"'s are not highlighted like int, double, etc. I will work on this problem more in the morning. I will post another edit tomorrow some time. To anyone that wants to help, this is what the code is suppose to do:

Allow the user to enter name-score pairs. For each student taking a test, the user types the student's name followed by the student's integer test score. The sorting function so it takes an array holding the student names, and an array holding the student test scores. When the sorted list of scores is displayed, each student's name should be displayed along with his or her score. In stepping through the arrays, use pointers rather than array subscripts.
Last edited on
I'm still having a little problem on making the the name and number arrays lining up. Above is the description. This is the code that I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>

using namespace std;

int* getNumber(int&);  //Allows user to input numbers, returns the pointer to data array with reference to size
string* getName(int&);  //Allows user to input names, returns the pointer to data array with reference to size
void sortNumber(int*, int);  //Sorts the input data into ascending order
void displayData(int*, string*, int);  //Displays sorted data and the students' names 

int main()
{
	int size;

	int* iptr = getNumber(size);  //Allows user to allocate memory and interactively enter data
	string* nptr = getName(size);  //Allows user to allocated memory and interactively enter data
	sortNumber(iptr, size);
	displayData(iptr, nptr, size);

	delete[] iptr; //Deallocate dynamic memory
	delete[] nptr; //Deallocate dynamic memory
	cout << endl;
	cout << " Type any key to continue--> ";
	_getch();

	return 0;
}

//Define getNumber() function
int* getNumber(int& size)
{

	do
	{
		cout << " How many text scores (positive whole numbers only): ";
		cin >> size;
		cout << endl;
	} 
	
	while (size <= 0);

	int* iptr = new int[size]; //Allocate a block of memory

	//Data entry loop
	for (int i = 0; i < size; i++)
	{
		do
		{
			cout << " Enter a valid test score: ";
			cin >> *(iptr + i);
			cout << " ______________________________";
			cout << endl;
		} 

		while (*(iptr + i) < 0);
	}

	return iptr; //Return pointer to allocated memory
}

//Define getName() function
string* getName(int& size)
{

	string* nptr = new string[size];

	//Data entry loop
	for (int i = 0; i < size; i++)
	
		{
			cout << " Enter a student's name: ";
			cin >> *(nptr + i);
			cout << " ______________________________";
			cout << endl;
		}


	return nptr; //Return pointer to allocated memory
}

//Define displayData() function
void displayData(int* iptr, string* nptr, int size)
{
	cout << " Test scores:  ";
	for (int i = 0; i < size; i++)
	{
		cout << *(iptr + i) << " ";

	}
	cout << endl;
	cout << " Students' names:  ";
	for (int i = 0; i < size; i++)
	{
		cout << *(nptr + i) << " ";
	}
	
}

//Define sortNumber() function
void sortNumber(int* iptr, int size)
{
	//This function sorts the data set using the selection sort algorithm
	int bottom = 0, top = size - 1, min, temp, minPosition;

	while (bottom < top)
	{
		min = *(iptr + bottom);  //The initial minimum value is at the bottom of the array
		minPosition = bottom;  //The initial position in array of minimum value

		for (int i = bottom + 1; i <= top; i++)  //This loop determines the minimum value in list from bottom to top and its position in the list
		{

			if (*(iptr + i) < min)
			{
				min = *(iptr + i);
				minPosition = i;
			}
		}
		//Swap bottom with new minimum value
		temp = *(iptr + bottom);
		*(iptr + bottom) = min;
		*(iptr + minPosition) = temp;

		bottom++;  //Increment bottom to new selection of array
	}
}
Last edited on
Topic archived. No new replies allowed.