【问题标题】:My C++ program crashed whenever I try to read the string每当我尝试读取字符串时,我的 C++ 程序就会崩溃
【发布时间】:2021-08-27 00:55:36
【问题描述】:

这是我的代码:

#include<iostream>
#include<string.h>
#define SIZE 100

struct person{
  std::string name;
  int age;
};

void entry(struct person *info){
  std::getline(std::cin, info->name);
  std::cin >> info->age;
}

int main(int argc, char const *argv[]) {
  struct person roster[SIZE];
  int n; // number of people in the roster:
  std::cin >> n;
  for (int i = 0; i < n; i++){
    entry(&roster[i]);
  }
  return 0;
}

我正在学习如何在 C++ 中使用“struct”,在这个程序中,我创建了一个包含姓名和年龄的名单,但是每当我尝试读取字符串“name”时程序就会崩溃。你能帮助我吗?谢谢,我坚持了好几天。

P.s:我正在用 C-book 学习 c++,所以我的代码可能包含 C-ism。

【问题讨论】:

  • “我正在用 C 书学习 c++”——请不要。它们是非常不同的语言
  • 您能否也显示您提供给程序的输入?它可能有助于了解您的程序崩溃的确切位置。
  • 你说>。你的输入是什么?例如,是否类似于:“2frank10johnni12”?您的程序没有明显的问题,因此您需要提供您在提示中键入的文本。

标签: c++ string struct


【解决方案1】:

请不要介意我在这里介绍的用户打印。有了它们,执行流程就更加清晰了。

因此,您应该关注以下内容:

#include<iostream>
#include<string.h>
#define SIZE 100

struct person{
    std::string name;
    int age;
};

void entry(struct person *info){
    std::cout << "Enter name: ";
    std::getline(std::cin, info->name);
    std::cout << "\nAge: ";
    std::cin >> info->age;
    std::cin.ignore();
    std::cout << std::endl;
}

int main(int argc, char const *argv[]) {
    person roster[SIZE];
    int n; // number of people in the roster:
    std::cout << "Number of people in roster: ";
    std::cin >> n;
    std::cin.ignore();
    for (int i = 0; i < n; i++){
        entry(&roster[i]);
    }
}

例子:

Number of people in roster: 3
Enter name: Foo
Age: 22

Enter name: Bar
Age: 33

Enter name: Baz
Age: 44

您遇到的主要问题具有复杂性,其根源在于 getline() 行为。我建议您查看此thread 以获得更多详细信息。

std::cin.ignore() 是什么,请查看documentation。关于何时以及如何使用它,请查看thread

【讨论】:

    【解决方案2】:

    声明struct后,要声明变量,只需在声明intdouble时做同样的事情:

    (struct) <struct_name> <variable_name> = <value>;
    

    这就是 C 和 C++ 的不同之处。在 C++ 中,struct 关键字在变量声明之前是可选的。在 C 中,它是强制性的。这就是为什么

    我正在用一本 C 书学习 c++

    是一个很大的数字。虽然它们的一些语法相似,但其余的却大不相同。如果您正在学习 C++,请查找相应的课程/书籍/视频。它们很丰富。

    对于the getline() problem,您可以包含cin.ignore(),以便程序忽略该行的其余字符并转到新行。

    #include<iostream>
    #include<string.h>
    #define SIZE 100
    
    struct person{
        std::string name;
        int age;
    };
    
    void entry(person *info){
        std::cin.ignore(); //ignores every character until next line
        std::getline(std::cin, info->name);
        std::cin >> info->age;
    }
    
    int main(int argc, char const *argv[])
    {
        person roster[SIZE];
        int n; // number of people in the roster:
        std::cin >> n;
        for (int i = 0; i < n; i++)
        {
            entry(&roster[i]);
        }
    
        for (int i = 0; i < n; i++)
        {
            person cur = roster[i];
            std::cout << cur.name << " " << cur.age << "\n";
        }
        return 0;
    }
    

    结果:

    2
    Beck
    8
    John Doe
    25
    Beck 8
    John Doe 25
    

    【讨论】:

    • @stuff_and_stuff 记住,找一本真正的 C++ 书来用 C++ 正确编码?
    • 我很感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多