简体   繁体   English

如何创建一个向结构成员读取/写入数据的函数?

[英]How to create a function to read/write data to struct members?

I'm trying to create a program that read and print students' data with c++. 我正在尝试创建一个程序,该程序使用c ++读取和打印学生的数据。 for that, I've created a struct Student, a function to read data from the user and assign it to a struct instance s1 and a function to print students' data on the screen, and I think the problem is with the function that read/write data. 为此,我创建了一个结构学生,一个从用户读取数据并将其分配给结构实例s1的函数,以及一个在屏幕上打印学生数据的函数,我认为问题在于读取函数/写入数据。

Here is the my code: 这是我的代码:

#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;
}

The output of this code is: 此代码的输出是:

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 . . .

As you see, the output is out of my expectations :) .. 如您所见,输出超出了我的期望:) ..

Any help ?! 有帮助吗?

Both CinCout and David are correct. CinCout和David都是正确的。 There are a couple of problems with your code as it now stands. 现在,您的代码存在两个问题。 The first problem is that while you successfully call the function read_data(s1) , s1 is a just a copy. 第一个问题是,当您成功调用函数read_data(s1) ,s1只是一个副本。 So, when the function sets all of the values for the student using cin, it is really just setting a copy's values. 因此,当函数使用cin为学生设置所有值时,实际上只是在设置副本的值。 You can either make it so that you are passing in the original, or you can return the student (which you are doing) and set s1 equal to the result (which you are not). 您可以这样做,以便传递原件,也可以返回学生(正在做),并将s1设置为等于结果(不是)。

To make sure that you pass in the original, you can go to where you declared read_data. 为了确保传递原件,可以转到声明为read_data的位置。 Instead of saying Student read_data(Student x) , you should place an ampersand after the parameter that you don't want to copy Student read_data(Student &x) . 与其说出Student read_data(Student x) ,不如不希望将其复制到要复制Student read_data(Student &x)的参数后面。 This is called passing by reference (you reference the original instead of referencing by copy) 这称为通过引用传递(您引用原始文件而不是通过副本引用)

Alternatively, you could con just set s1 to the result where you call it in main. 或者,您可以将s1设置为在main中调用它的结果。 You could say s1 = read_data(s1); 您可以说s1 = read_data(s1); and that would work fine, though a bit more inefficiently. 效果很好,尽管效率低一些。

Lastly, the other glaring error in the code is that you accidentally call read_data again when you say print_data(read_data(s1)) . 最后,代码中另一个明显的错误是您在说出print_data(read_data(s1))时意外地再次调用read_data。 Instead, say print_data(s1) . 而是说print_data(s1)

Instead of passing and returning the structure object each time on call of read_data and print_data we could add those inside the structure itself, We could create object of Student and call the functions read and print within the same. 不必在每次调用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;
}

You are passing copy of s1 into the read_data function, but not bothering to update the value based on the return arg. 您正在将s1的副本传递到read_data函数中,但不必费心根据返回arg更新值。 ie something like this should work. 即这样的事情应该工作。

s1 = read_data(s1);
print_data(s1);

Alternatively, pass by reference instead of value: 或者,通过引用而不是值传递:

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;
}

And then later: 然后再:

read_data(s1);
print_data(s1);

change you read_data with something like this 用这样的东西改变你的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
}

and in main function or where you are calling the read_data function use 在主函数中或在调用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);

the reason you are getting weird values in return is that you capture buffer with cin >> instead getline see 您得到奇怪值的原因是您使用cin >>捕获了缓冲区,而不是getline看到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM