我正在尝试创建一个程序,该程序使用c ++读取和打印学生的数据。为此,我创建了一个结构学生,一个从用户读取数据并将其分配给结构实例s1的函数,以及一个在屏幕上打印学生数据的函数,我认为问题在于读取函数/写入数据。
这是我的代码:
#include<iostream>
#include<string>
using namespace std;
struct Student
{
char name[30];
int age;
double gpa;
string department;
};
Student read_data(Student x)
{
cout << "Name (30 characters maximum): ";
cin.get(x.name, 30);
cout << "Age: ";
cin >> x.age;
cout << "Department: ";
cin >> x.department;
cout << "GPA: ";
cin >> x.gpa;
return x;
}
void print_data(Student x)
{
cout <<
"\n***************************************************************" << endl;
cout << "Name: " << x.name << endl;
cout << "Age: " << x.age << endl;
cout << "Department: " << x.department << endl;
cout << "GPA: " << x.gpa << endl;
}
int main()
{
Student s1, s2, s3;
cout << "This program stores -Temporarily- data of three students\n" << endl;
cout << "Enter 1st student's data" << endl;
read_data(s1);
print_data(read_data(s1));
system("pause");
return 0;
}
此代码的输出是:
This program stores data of three students
Enter 1st student's data
Name (30 characters maximum): Ahmed Maysara
Age: 22
Department: CS
GPA: 3.5
Name (30 characters maximum): Age: Department: GPA:
***************************************************************
Name:
Age: -858993460
Department:
GPA: -9.25596e+61
Press any key to continue . . .
如您所见,输出超出了我的期望:) ..
有帮助吗?!
答案 0 :(得分:1)
CinCout和David都是正确的。
现在,您的代码存在两个问题。
第一个问题是,当您成功调用函数read_data(s1)
时,s1只是一个副本。因此,当函数使用cin为学生设置所有值时,实际上只是在设置副本的值。您可以这样做,以便传递原件,也可以返回学生(正在做),并将s1设置为等于结果(不是)。
要确保传递原件,可以转到声明为read_data的位置。与其说Student read_data(Student x)
,不如不要复制Student read_data(Student &x)
,请在符号后面加上一个&号。这称为通过引用传递(您引用原始文件而不是通过副本引用)
或者,您可以将s1设置为在main中调用它的结果。您可以说s1 = read_data(s1);
,虽然效率不高,但效果很好。
最后,代码中另一个明显的错误是您在说print_data(read_data(s1))
时不小心再次调用read_data。而是说print_data(s1)
。
答案 1 :(得分:1)
我们不必在每次调用read_data和print_data时都传递和返回结构对象,而是可以在结构本身内部添加这些对象,而可以创建Student对象并在同一函数中调用read和print函数。
struct Student
{
char name[30];
int age;
double gpa;
string department;
Student(): age(0), gpa(0)
{
memset( name, 0, 30 );
}
void read()
{
cout << "\nName (30 characters maximum): ";
cin.get(name, 30);
cout << "\nAge: ";
cin >> age;
cout << "\nDepartment: ";
cin >> department;
cout << "\nGPA: ";
cin >> gpa;
}
void print()
{
cout << "\n***************************************************************" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Department: " << department << endl;
cout << "GPA: " << gpa << endl;
}
};
int main()
{
Student s1;
s1.read();
s1.print();
return 0;
}
答案 2 :(得分:0)
您正在将s1的副本传递给read_data函数,但不必费心根据返回arg更新值。即类似这样的东西应该起作用。
s1 = read_data(s1);
print_data(s1);
或者,通过引用而不是值:
void read_data(Student& x)
{
cout << "Name (29 characters maximum): "; // requires null terminator
cin >> x.name; // just read into the buffer directly
cout << "Age: ";
cin >> x.age;
cout << "Department: ";
cin >> x.department;
cout << "GPA: ";
cin >> x.gpa;
}
再后来:
read_data(s1);
print_data(s1);
答案 3 :(得分:0)
使用类似这样的内容更改您的read_data
void read_data(Student& x)
{
cout << "Name (30 characters maximum): ";
///cin.get(x.name, 30);
cin.getline(x.name, 30);
cout << "Age: ";
cin >> x.age;
cin.ignore();
cout << "Department: ";
std::getline(cin, x.department);
///cin >> x.department;
cout << "GPA: ";
cin >> x.gpa;
cin.ignore();
// return x; can't return a value from a void function
}
在主函数中或在调用read_data函数的地方使用
Student s1, s2, s3;
cout << "This program stores -Temporarily- data of three students\n" << endl;
cout << "Enter 1st student's data" << endl;
read_data(s1);
read_data(s2);
read_data(s3);
获得奇怪值的原因是您使用cin >>而不是getline捕获缓冲区 见