简体   繁体   English

调用函数,只返回0

[英]Calling for function, only returning 0

Alrighty, the goal of what I am trying to do right now is call the function getSingleStudentInfo, which contains the student's number, last name, and age.好吧,我现在要做的目标是调用函数 getSingleStudentInfo,其中包含学生的编号、姓氏和年龄。 In the end this program is designed to do two things, the first being the single student info, the second, printing out an array of 20 students.最后,该程序旨在做两件事,第一件事是单个学生信息,第二件事是打印出 20 名学生的数组。 Disregard the second part, as I have not really gotten into that part yet, so ignore anything involving vectors.忽略第二部分,因为我还没有真正进入那部分,所以忽略任何涉及向量的内容。

The problem that I am having is that in main the first thing that the program will do is ask you to press 1 for the single info or 2 for the full 20 peoples info.我遇到的问题是,该程序首先要做的第一件事是要求您按 1 获取单个信息或按 2 获取完整的 20 个人信息。 The program compiles fine, but what happens is, no matter what number you enter, the program will say "process returned 0 (0x0)" and be done, I'm having a hard time figuring out why it is doing that instead of printing out the single students info, being "student's ID number is 400" "student's last name is: Simmons" "student's age is: 20"程序编译正常,但发生的情况是,无论您输入什么数字,程序都会说“进程返回 0 (0x0)”并完成,我很难弄清楚为什么要这样做而不是打印出单身学生信息,是“学生的身份证号码是400”“学生的姓氏是:西蒙斯”“学生的年龄是:20”

#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct Student {
    int studentNumber = 400;
    string lastName = "Simmons";
    int age = 20;
};

Student s;
int selection;
vector<int> studentNumber (20);
vector<string> lastName;
vector<int> age (20);

void getSingleStudentInfo (int studentNumber, string lastName, int age) {
    cout << "Student's ID number is: ";
    cout << s.studentNumber << endl;
    cout << "Student's last name is: ";
    cout << s.lastName << endl;
    cout << "Student's age is: ";
    cout << s.age << endl;
return;
};

int main()
{
    cout << "Press '1' to see a single student data entry" << endl;
    cout << "Press '2' to see all 20 student records" << endl;
    cin >> selection;
    if (selection == 1) {
    getSingleStudentInfo;
    };
    /*for (vector<int>::size_type i = 0; i <= 20; i++)
    {
        cout << "Student's ID number is: " << 400 + i << endl;
    }
    return 0;*/
}

You need to call the function, eg您需要调用该函数,例如

if (selection == 1)
{
    getSingleStudentInfo(7, "Johnson", 20);
}

However, it seems like by the implementation, this should be a method off of the student itself但是,从实现来看,这应该是学生本身的一种方法

struct Student {
    int studentNumber = 400;
    string lastName = "Simmons";
    int age = 20;
    void getSingleStudentInfo() const;
};

Then you'd call it off a Student instance然后你可以从Student实例中调用它

Student s{400, "Simmons", 20};
s.getSingleStudentInfo();

Then if you had a vector of Student you could do那么如果你有一个Student的向量,你可以做

std::vector<Student> students; // assume this has been populated
std::for_each(begin(students),
              end(students),
              [](const Student& s){s.getSingleStudentInfo();});

To print in columns, you could change your function to something like要按列打印,您可以将函数更改为类似

void Student::getSingleStudentInfo()
{
    cout << s.studentNumber << '\t'
         << s.lastName << '\t'
         << s.age << endl;
};

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

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