[英]How to create a function to read/write data to struct members?
我正在尝试创建一个程序,该程序使用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 . . .
如您所见,输出超出了我的期望:) ..
有帮助吗?
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)
。
不必在每次调用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;
}
您正在将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);
用这样的东西改变你的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看到了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.