c++ - cin.getline() 使程序崩溃

标签 c++ crash getline

我对使用 C++ 还很陌生,所以我不完全确定我的程序出了什么问题。每次我运行它时,它都会在 cin.getline() 之后崩溃,我已经在其他地方询问过,但似乎没有明显的原因说明它不起作用。该代码旨在读取用户输入并将其存储在链接列表中,然后搜索或删除列表中的条目。

#include <iostream>
#include <cstring>

using namespace std;

struct node{
       char name[20];
       char age[4];
       node *next;
};

class personList{
      public:
             void add_person(char name[20], char age[4]);
             void remove_person(char name[20]);
             node *search_people(char name[20]);

      protected:
                void add_node(char name[20], char age[4]);
                void remove_node(char name[20]);
                node *search_nodes(char name[20]);
                node *root;
                node *position;        
};

void personList::add_person(char name[20], char age[4]){
     add_node(name, age);
}

void personList::add_node(char name[20], char age[4]){
     if (position == NULL)
     {
                 root = new node;
                 strcpy(root->name, name);
                 strcpy(root->age, age);
                 root->next = NULL;
                 position = root;
     }
     else
     {
                position->next = new node;
                position = position->next;
                strcpy(position->name, name);
                strcpy(position->age, age);
                position->next = NULL;
     }
}

void personList::remove_person(char name[20]){
     remove_node(name);
}

void personList::remove_node(char name[20]){
     node *targ;
     targ = search_nodes(name);
     if (targ != NULL)
     {
              delete targ;
     }
}

node *personList::search_people(char name[20]){
     position = root;
     return search_nodes(name);
}

node *personList::search_nodes(char name[20]){
    while(strcmp(position->name, name) !=0 && position->next != NULL)
    {
                          position = position->next;
    }
    if (strcmp(position->name, name) == 0)
    {
                        return position;
    }
    else
    {
        return NULL;    
    }
}

int main(){
    personList database;
    int inp = 1;
    char name[20];
    char age[4];
    node *search_result;
    while (inp != 4)
    {
          cout << "list of commands:\n1. add person\n2. remove person\n3.search for person\n4. exit\n> ";
          cin.get() >> inp;
          switch (inp)
          {
                 case 1:
                      cout << "input the name and age of the person you wish to add:\n";
                      cin.getline(name, 20, '\n');
                      strcat(name, "\n");
                      cin.getline(age, 4, '\n');
                      strcat(age, "\n");
                      database.add_person(name, age);
                 case 2:
                      cout << "input the name of the person you wish to remove:\n";
                      cin.getline(name, 20, '\n');
                      database.remove_person(name);
                 case 3:
                      cout << "input the name of the person you wish to search for:\n";
                      cin.getline(name, 20, '\n');
                      search_result=database.search_people(name);
                      if (search_result == NULL)
                      {
                                        cout << "the person you searched for does not exist in this database\n";
                      }
                      else
                      {
                          cout << name << " is in the database as being " << search_result->age << " years old";
                      }
                 case 4:
                      break;
                 default:
                         cout << "bad input, please enter a number";
          }
    }      
}

据我运行时所见,它是底部第一个案例中的第一个 cin.getline() 。我一按回车键,程序就崩溃了。

         case 1:
              cout << "input the name and age of the person you wish to add:\n";
              cin.getline(name, 20, '\n'); //this line causes the crash
              strcat(name, "\n");          //as far as I understand
              cin.getline(age, 4, '\n');
              strcat(age, "\n");
              database.add_person(name, age);

对于我做错的事情的一般性指示,我们将不胜感激。

谢谢!

最佳答案

这是代码的重要部分:

char name[20];
//...
cin.getline(name, 20, '\n');
strcat(name, "\n");

cin.getline 名称之后可以包含 20 个字符,包括终止空字符。因此,您不能只是向其中添加另一个 "\n",因为缓冲区大小只是 20,而不是 21。

除此之外,如果你的程序达到了

database.add_person(name, age);

还有其他问题;您调用 add_node,它的开头如下:

void personList::add_node(char name[20], char age[4]){
    if (position == NULL)

Bug position 从未初始化,personList 甚至没有构造函数,因此它可能不为 null,但也无效。

关于c++ - cin.getline() 使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714616/

相关文章:

c++ - 使用枚举参数重载模板时出现 MSVC 编译器错误

C++在给程序一个.txt后从键盘读取(在in之后使用getline())

c++ - 从文件中读取行并存储在单独的字符串变量中

C++ Visual Studio 非标准语法使用 '&' 创建指向成员的指针

c++ - 在 Objective C++ .mm 文件中包含 POCO 头文件会导致 Xcode 6 出现编译错误

c++ - 将所有类实例的列表存储为类中的静态数组

包含任何类型的资源时,Android aapt 崩溃

MySQL ...开始崩溃恢复

logging - Umbraco数据库日志错误:找不到任何nodeId = 0的页面

带分隔符的C++文件读取