Hi Peter, Thanks for the input. That was a silly mistake on my part which I had overlooked. However, that was not the cause of the problem. I am thinking it has something to do with the area around 'cin >> data.age;' in the 'student_info' function, as this is where the problem occurs. However, I don't yet have the experience to pinpoint it. Thanks Gedi ---------------------------------- From: Peter Morreale ---------------------------------- You test in the "for" loops is wrong, it should be: for(i = 0; i < MAX_STU; i++) ^^^^^^^^^^^ By saying "<=" you are accessing students[5] which is out-of-bounds. Recall that arrays start from zero, not 1. -PWM Gedi wrote:
Hi all,
I am very new to programming and am currently learning C++. At the moment I'm at the stage of structures and classes, so am writing many programs oriented around this to help. I have written a simple problem which I cannot get to run.
Usually I have no big problems as the compiler aids me in debugging, however this program compiles and I am not up to the stage of using a debugging tool like GDB yet.
My program seems to jump after asking the age and when complete gives very strange outputs.
I can't paste an error report with this as there is no error, hopefully somebody can show me where I'm going wrong.
Code as follows:
#include <iostream.h> #include <string.h>
struct students student_info(struct students data); void print(struct students data);
#define MAX_STU 5
struct students { char name[30]; int age; char grade[2]; int iq; };
main() {
students student[MAX_STU]; int i;
//get the data for (i=1; i<=MAX_STU; ++i) { student[i] = student_info(student[i]); }
//print the data for (i=1; i<=MAX_STU; ++i) { print(student[i]); } }
struct students student_info(struct students data) { cout << "what is the students name: "; cin.getline (data.name, 30); cout << "What is the students age: "; cin >> data.age; cout << "What is the students grade: "; cin.getline (data.name, 30); cout << "What is the students IQ: "; cin >> data.iq;
return (data); }
void print(struct students data) { cout << "Name: " << data.name << endl; cout << "Age: " << data.age << endl; cout << "Grade: " << data.grade << endl; cout << "IQ: " << data.iq << endl << endl;
}
thanks
Gedi
-- Peter W. Morreale email: morreale@radiantdata.com Director of Engineering Niwot, Colorado, USA Radiant Data Corporation voice: (303) 652-0870 x108 ------------------------------------------------------------------------ ----- This transmission may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.
student.grade is uninitialized, because you do: cin.getline (data.name, 30); twice instead of: cin.getline (data.grade, 2); struct students should be initialized. Kai-Uwe -----Ursprüngliche Nachricht----- Von: Gedi [mailto:gedi@ntlworld.com] Gesendet: Dienstag, 4. November 2003 00:02 An: 'SuSE Programming' Cc: morreale@radiantdata.com Betreff: RE: [suse-programming-e] c++ problem Hi Peter, Thanks for the input. That was a silly mistake on my part which I had overlooked. However, that was not the cause of the problem. I am thinking it has something to do with the area around 'cin >> data.age;' in the 'student_info' function, as this is where the problem occurs. However, I don't yet have the experience to pinpoint it. Thanks Gedi ---------------------------------- From: Peter Morreale ---------------------------------- You test in the "for" loops is wrong, it should be: for(i = 0; i < MAX_STU; i++) ^^^^^^^^^^^ By saying "<=" you are accessing students[5] which is out-of-bounds. Recall that arrays start from zero, not 1. -PWM Gedi wrote:
Hi all,
I am very new to programming and am currently learning C++. At the moment I'm at the stage of structures and classes, so am writing many programs oriented around this to help. I have written a simple problem which I cannot get to run.
Usually I have no big problems as the compiler aids me in debugging, however this program compiles and I am not up to the stage of using a debugging tool like GDB yet.
My program seems to jump after asking the age and when complete gives very strange outputs.
I can't paste an error report with this as there is no error, hopefully somebody can show me where I'm going wrong.
Code as follows:
#include <iostream.h> #include <string.h>
struct students student_info(struct students data); void print(struct students data);
#define MAX_STU 5
struct students { char name[30]; int age; char grade[2]; int iq; };
main() {
students student[MAX_STU]; int i;
//get the data for (i=1; i<=MAX_STU; ++i) { student[i] = student_info(student[i]); }
//print the data for (i=1; i<=MAX_STU; ++i) { print(student[i]); } }
struct students student_info(struct students data) { cout << "what is the students name: "; cin.getline (data.name, 30); cout << "What is the students age: "; cin >> data.age; cout << "What is the students grade: "; cin.getline (data.name, 30); cout << "What is the students IQ: "; cin >> data.iq;
return (data); }
void print(struct students data) { cout << "Name: " << data.name << endl; cout << "Age: " << data.age << endl; cout << "Grade: " << data.grade << endl; cout << "IQ: " << data.iq << endl << endl;
}
thanks
Gedi
-- Peter W. Morreale email: morreale@radiantdata.com Director of Engineering Niwot, Colorado, USA Radiant Data Corporation voice: (303) 652-0870 x108 ------------------------------------------------------------------------ ----- This transmission may contain information that is privileged, confidential and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. -- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
participants (2)
-
Gedi
-
Kai-Uwe Schmidt