Hi Jerry, I have just started on pointers and passing by address a few days ago. The first problem you pointed out was a silly mistake on my part. I should have noticed that. I guess spotting silly mistakes like that will become second nature in time. The second issue makes sense now. Thanks for taking the time to explain Regards, Ged. **(for reference, I have changed my mail address for this list as it was becoming cluttered and hard to manage)** On Saturday 08 November 2003 14:24, Jerry Feldman wrote:
On Mon, 3 Nov 2003 22:33:46 -0000
"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
#include 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;
}
Here is one big problem: for (i=1; i<=MAX_STU; ++i) { C and C++ arrays are always indexed relative to 0. So, in this, you mist the first element in your student array, and try to populate student[MAX_STU], which is beyond the end of your array. The fix for this is: for (i=0; i < MAX_STU; ++i) { Note start i at 0 and test for i < MAX_STU not <=.
The second issue is that you are passing the entire struct students into both student_info() and print(). This is a copy operation. Additionally, you are copying the structure back to the students struct. While this is not wrong, it is very inefficient. Since student_info() initializes the array, you can easily avoid the copying in by: struct students student_info() { struct students data;
Since you may not have done pointers and the C++ pass by reference, I will not comment further on the efficiancy issue.
-- Jerry Feldman
Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9