Two Miscellaneous Questions

Okay, so here is the code I have in my program:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace 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.

lineFirst = alphabetFirst(&studentName[numStudents], numStudents);

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:

lineFirst = alphabetFirst(studentName, numStudents);

Or... if you want to be more verbose, you can do this:


lineFirst = alphabetFirst(&studentName[0], numStudents);
Last edited on
Thanks!

EDIT: With changes made, the second problem still persists.
Last edited on
Arrays don't resize
1
2
    unsigned numStudents; //¿value?
    string tempString, studentName[numStudents], //¿size of the array? 
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.


Then it returns 3.

What did I do?
Last edited on
Is your code updated? I wanna help, but don't wanna read an outdated question.
My updated code looks like this:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace 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, numInput = 0;
    string tempString, *studentName;
    string lineLast, lineFirst;
    studentFile.open("LineUp.txt");
    if(!studentFile)
    {
        cout<<"Error opening file. Restart program.";
        cin.get();
        return 1337;
    }
    else
    {
        while(studentFile>>tempString)
        {
            numStudents++;
        }
        studentName = new string[numStudents];
        studentFile.close();
        studentFile.open("LineUp.txt");
        while(studentFile>>tempString)
        {
            numInput++;
            studentName[numInput] = tempString;
        }
        lineFirst = alphabetFirst(studentName, numStudents);
        lineLast = alphabetLast(studentName, 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;
}


And as I said, it gets mad at me and then returns 3. Any ideas?
1
2
3
4
5
6
std::ifstream input("LineUp.txt");
std::vector<std::string> names;

std::string temp;
while(cin>>temp)
   names.push_back(temp);


Your alphabet{First,Last} are incorrect if the list is small (just one element)
You could simply use `std::{min,max}_element()'


Run through a debugger and perform a back trace.
Topic archived. No new replies allowed.