string alphabetFirst(string listNames, unsigned numNames)
unsigned positionFirst = 1;
for(int test = 0; test < numNames; test++)
if(listNames[test] < listNames[positionFirst])
positionFirst = test;
string alphabetLast(string listNames, unsigned numNames)
unsigned positionLast = 1;
for(int test = 0; test<numNames; test++)
if(listNames[test] > listNames[positionLast])
positionLast = test;
string tempString, studentName[numStudents], filename = "univhigh.txt";
string lineLast, lineFirst;
cout<<"Enter filename to read from: ";
cout<<"Error opening file. Restart program.";
studentName[numStudents] = tempString;
lineFirst = alphabetFirst(&studentName[numStudents], numStudents);
lineLast = alphabetLast(&studentName[numStudents], numStudents);
cout<<"The student who should stand first in line is: "<<lineFirst<<endl;
cout<<"The student who should stand last in line is: "<<lineLast<<endl;
My questions are: 1. Why does my compiler (Code::Blocks 10.05) require me to pass the addresses of the array studentname[numstudents] in my alphabetFirst() and alphabetLast() functions? In my function declarations, I declared them using strings, not pointers.
2. Why, when I compile and run this program, do I get no compiler errors (and no warnings either), but get a "<program_name>.exe has stopped working..." message box and a blank console window?
Thank you in advance for your help.
numStudents is a bad index (it's passed the end of the array), so studentName[numStudents] is out of bounds. Which means &studentName[numStudents] is a bad pointer. Passing a bad pointer is causing bad memory accesses, which is causing your program to crash.
What you want is to pass a pointer to the first string in the array. That can be done easily by just using the array name:
Okay, so what if I were to change that to this: string *studentName;
and then later, say after I know the number of students in the text file, put in studentName = new string[numStudents];
and go on with my code. Would that work?
EDIT: okay, scratch that. Now it returns a message saying
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.