help please

Writing a program using pointers instead of arrays. im wrinting a program that ask for the number of stuedents. then dynamically allocate an array(one to hold the name(string) and one to hold there test score). Past that to a function that puts the array in from lowest to biggest. and then i display them from biggest to smallest. Problem is when the arguments are being passed to the "sortingArray" function, the program just stops? help?


#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void header(int *, string *, int&);
//void sortingArry(int *, string *, int );
void sortingArry(int *, int);

int main()
{
int *testScore=0;
string *student= 0;
int numOfStudents=0;

header(testScore,student, numOfStudents);
//sortingArry(testScore, student, numOfStudents);
sortingArry(testScore, numOfStudents);
int place =1;
cout << "From highest to lowest score:\n";
/*for (int index =(numOfStudents-1); index>=0; index--)
{
cout << place << ": " << *(student + index) << setw(5) << *(testScore +index) << endl;
place ++;
}*/

delete [] testScore;
delete [] student;

return 0;
}
void header(int *tS, string *student, int &size)
{
cout << "\t\tNameTestScore\n\n";
cout << "How many students are testing? ";
cin >> size;
cout << endl;
cin.ignore();

tS= new int[size];
student = new string[size];

for(int index =0; index< size; index++)
{
cout << "Enter students name: ";
getline(cin, *(student + index));
cout << "Enter " << *(student + index) << " score: ";
cin >> *(tS + index);
cin.ignore();
}
}
/*void selectionSort(int *scores, string *stuName, int size)
{
int startScan, minIndex, minValue;
string minName;
for(startScan= 0; startScan< (size-1); startScan++)
{
minIndex= startScan;
minValue=*(scores + startScan);
minName = *(stuName + startScan);
for(int index =startScan+1; index<size; index++)
{
if(*(scores +index) <minValue)
{
minValue= *(scores +index);
minName = *(stuName+index);
}
}
*(scores + minIndex) = *(scores +startScan);
*(stuName + minIndex) = *(stuName + startScan);
*(scores +startScan) = minValue;
*(stuName + startScan) = minName;
}
}
void sortingArry(int *testscore, string *name, int size)
{
int startScan, minIndex;
int minValue;
//string minName;
for(startScan=0; startScan< (size-1); startScan++)
{
minIndex=startScan;
minValue= *(testscore+startScan);
//minName = *(name + startScan);
for(int index=startScan+1; index <size; index++)
{
if(testscore[index]<minValue)
{
minValue=testscore[index];
//minName= *(name + index);
minIndex=index;

}
}
testscore[minIndex]=testscore[startScan];
//*(name+minIndex) = *(name +startScan);
testscore[startScan]=minValue;
//*(name+startScan) = minName;
}

}*/
void sortingArry(int *testscore, int size)
{
int startScan, minIndex;
int minValue;
for(startScan=0; startScan< (size-1); startScan++)
{
minIndex=startScan;
minValue=testscore[startScan];
for(int index=startScan+1; index <size; index++)
{
if(testscore[index]<minValue)
{
minValue=testscore[index];
minIndex=index;
}
}
testscore[minIndex]=testscore[startScan];
testscore[startScan]=minValue;
}
cout << "\nTest score ascending order are : ";
for (int index =0; index<size; index++)
cout << testscore[index] << " ";
cout << endl<< endl;
}
closed account (48bpfSEw)
could you please use code beautifyer next time and put your code between "[ code ]" "[ /code ]"?

http://www.tutorialspoint.com/online_c_formatter.htm


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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void            header(int *, string *, int &);
// void sortingArry(int *, string *, int );
void            sortingArry(int *, int);

int
main()
{
    int            *testScore = 0;
    string         *student = 0;
    int             numOfStudents = 0;

    header(testScore, student, numOfStudents);
    // sortingArry(testScore, student, numOfStudents);
    sortingArry(testScore, numOfStudents);
    int             place = 1;
    cout << "From highest to lowest score:\n";
    /*
     * for (int index =(numOfStudents-1); index>=0; index--) { cout <<
     * place << ": " << *(student + index) << setw(5) << *(testScore
     * +index) << endl; place ++; }
     */

    delete[]testScore;
    delete[]student;

    return 0;
}
void
header(int *tS, string * student, int &size)
{
    cout << "\t\tNameTestScore\n\n";
    cout << "How many students are testing? ";
    cin >> size;
    cout << endl;
    cin.ignore();

    tS = new int[size];
    student = new string[size];

    for (int index = 0; index < size; index++) {
	cout << "Enter students name: ";
	getline(cin, *(student + index));
	cout << "Enter " << *(student + index) << " score: ";
	cin >> *(tS + index);
	cin.ignore();
    }
}
/*
 * void selectionSort(int *scores, string *stuName, int size) { int
 * startScan, minIndex, minValue; string minName; for(startScan= 0;
 * startScan< (size-1); startScan++) { minIndex= startScan;
 * minValue=*(scores + startScan); minName = *(stuName + startScan);
 * for(int index =startScan+1; index<size; index++) { if(*(scores +index)
 * <minValue) { minValue= *(scores +index); minName = *(stuName+index); }
 * } *(scores + minIndex) = *(scores +startScan); *(stuName + minIndex) =
 * *(stuName + startScan); *(scores +startScan) = minValue; *(stuName +
 * startScan) = minName; } } void sortingArry(int *testscore, string
 * *name, int size) { int startScan, minIndex; int minValue; //string
 * minName; for(startScan=0; startScan< (size-1); startScan++) {
 * minIndex=startScan; minValue= *(testscore+startScan); //minName =
 * *(name + startScan); for(int index=startScan+1; index <size; index++) {
 * if(testscore[index]<minValue) { minValue=testscore[index]; //minName=
 * *(name + index); minIndex=index;
 * 
 * } } testscore[minIndex]=testscore[startScan]; //*(name+minIndex) =
 * *(name +startScan); testscore[startScan]=minValue; //*(name+startScan) = 
 * minName; }
 * 
 * }
 */
void
sortingArry(int *testscore, int size)
{
    int             startScan,
                    minIndex;
    int             minValue;
    for (startScan = 0; startScan < (size - 1); startScan++) {
	minIndex = startScan;
	minValue = testscore[startScan];
	for (int index = startScan + 1; index < size; index++) {
	    if (testscore[index] < minValue) {
		minValue = testscore[index];
		minIndex = index;
	    }
	}
	testscore[minIndex] = testscore[startScan];
	testscore[startScan] = minValue;
    }
    cout << "\nTest score ascending order are : ";
    for (int index = 0; index < size; index++)
	cout << testscore[index] << " ";
    cout << endl << endl;
}
Last edited on
closed account (48bpfSEw)


the problem is now obviously :

1
2
tS = new int[size];
student = new string[size];


you should use "pointer of pointer" to intialize tS and student

1
2
3
4
5
6
7
void
header(int **tS, string ** student, int &size)
{
...

    *tS = new int[size];
    *student = new string[size];



Thanks Necip, i didn't know about pointer of a pointer, I will have to read about that. Thanks
Topic archived. No new replies allowed.