M
Mathew McBride
Hello all,
I'm a self-taught java dev trying to teach (yet again) teach myself the
ins-n-outs of C++. I'm working on the problem from
http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/INTRODUCTION.html#youTry3
I haven't gotten far in figuring out the reason why my program crashes.
I've tried on three different platforms (cygwin, opensolaris and linux)
but can't squeeze a decent stack trace out of gdb (yes, using g++ -g -O0).
It happens after cout (i.e the last thing my program does), and some
stack traces I've got indicate a problem with ~Address - does this
indicate some sort of destructor for a stack like there are in classes?
Heres the source:
#import <iostream>
struct Address {
std::string city;
int zip;
};
struct Student {
int id;
bool isGrad;
Address addr;
};
int numOfGrads (Student std[], int nstd);
int main(int nargs, char *args[]) {
Student students[1];
students[0].id = 0;
students[0].isGrad = true;
students[0].addr.city = "Geelong";
students[0].addr.zip = 3200;
students[1].id = 1;
students[1].isGrad = false;
students[1].addr.city = "Werribee";
students[1].addr.zip = 7331;
int gr = numOfGrads(students, 1);
std::cout << "Number of students is: " << gr << std::endl;
return 0;
}
int numOfGrads(Student std[], int nstd) {
int num = 0;
int x;
for (x=0; x<nstd; x++) {
if (std[x].isGrad) {
num++;
}
}
return num;
}
I'm a self-taught java dev trying to teach (yet again) teach myself the
ins-n-outs of C++. I'm working on the problem from
http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/INTRODUCTION.html#youTry3
I haven't gotten far in figuring out the reason why my program crashes.
I've tried on three different platforms (cygwin, opensolaris and linux)
but can't squeeze a decent stack trace out of gdb (yes, using g++ -g -O0).
It happens after cout (i.e the last thing my program does), and some
stack traces I've got indicate a problem with ~Address - does this
indicate some sort of destructor for a stack like there are in classes?
Heres the source:
#import <iostream>
struct Address {
std::string city;
int zip;
};
struct Student {
int id;
bool isGrad;
Address addr;
};
int numOfGrads (Student std[], int nstd);
int main(int nargs, char *args[]) {
Student students[1];
students[0].id = 0;
students[0].isGrad = true;
students[0].addr.city = "Geelong";
students[0].addr.zip = 3200;
students[1].id = 1;
students[1].isGrad = false;
students[1].addr.city = "Werribee";
students[1].addr.zip = 7331;
int gr = numOfGrads(students, 1);
std::cout << "Number of students is: " << gr << std::endl;
return 0;
}
int numOfGrads(Student std[], int nstd) {
int num = 0;
int x;
for (x=0; x<nstd; x++) {
if (std[x].isGrad) {
num++;
}
}
return num;
}