#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
string alphabetFirst(string listNames[], unsigned numNames)
{
unsigned positionFirst = 1;
for(int test = 0; test < numNames; test++)
{
if(listNames[test] < listNames[positionFirst])
{
positionFirst = test;
}
}
return listNames[positionFirst];
}
string alphabetLast(string listNames[], unsigned numNames)
{
unsigned positionLast = 1;
for(int test = 0; test<numNames; test++)
{
if(listNames[test] > listNames[positionLast])
{
positionLast = test;
}
}
return listNames[positionLast];
}
int main()
{
ifstream studentFile;
unsigned numStudents;
string tempString, studentName[numStudents], filename = "univhigh.txt";
string lineLast, lineFirst;
cout<<"Enter filename to read from: ";
cin>>filename;
studentFile.open("univhigh.txt");
if(!studentFile)
{
cout<<"Error opening file. Restart program.";
cin.get();
return 1337;
}
else
{
while(studentFile>>tempString)
{
numStudents++;
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;
cin.get();
}
return 0;
}
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.
1) Your functions takes arrays of strings (or, technically, a pointer to an array of strings). Not a single string.
Compare: string alphabetFirst(string listNames[], unsigned numNames)
Where listNames is an array of strings. VS:
string alphabetFirst(string listName)
Where listName is a single string.
C (and therefore C++) does not really allow you to pass arrays to functions. So instead it works by passing a pointer to the first string in the array.
2) Because you're passing the pointer incorrectly.
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.