why it jump?
This is a example I got from website. I run it, the output is weird,
can anybody tell me the reason?
Thanks!
#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.grade, 2);
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;
}
|