c++ - 为什么它停止并以退出代码 11 结束?

标签 c++ loops

我不知道它为什么停在那里并以退出代码 11 结束。它应该一直运行到我发出命令为止。

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


void record(string name, string phoneNum, int count);

// main
int main() {
    cout << " Welcome to use the Phone Contact Systerm " << endl;
    string name;
    string phoneNum;
    int count = 0;
    string signToStop;
    cout << " Please enter name and phone number " << endl;
    while ( cin >> name >> phoneNum){
        cout << " If you want to start the program, enter start "         << endl;
        cout << " If you want to quit the program, enter quit " <<     endl;
        cin >> signToStop;
        if (signToStop == "start"){
            record(name, phoneNum, count);
            cout << " Please enter name and phone number " << endl;
        }
        else if ( signToStop == "quit" ){
            break;
        }
        cout << count << endl;
        count++;


    }
}

// record all name info into Name set and record all phone numbers         into PhoneNum set
void record(string name, string phoneNum, int count){
    string Name[] = {};
    string PhoneNum[] = {};
    Name[count] = {name};
    PhoneNum[count] = {phoneNum};

    // now start to record all the info into .txt document

    ofstream phoneFile;
    phoneFile.open("contact.txt");
    phoneFile << name << "  " << phoneNum << endl;
}

结果是:

 Welcome to use the Phone Contact Systerm 

 Please enter name and phone number 

Molly 5307609829

 If you want to start the program, enter start 

 If you want to quit the program, enter quit 

start

 Please enter name and phone number 

0

Lilyi 44080809829

 If you want to start the program, enter start 

 If you want to quit the program, enter quit 

start

Process finished with exit code 11

最佳答案

问题出在这部分:

void record(string name, string phoneNum, int count){
    string Name[] = {};
    string PhoneNum[] = {};
    Name[count] = {name};
    PhoneNum[count] = {phoneNum};

    //...
}

这在 C++ 中很糟糕,因为 string Name[] = {}; 和其他类似的东西并没有按照您认为的那样去做。他们创建一个空的字符串数组。自变量 length arrays are not a thing in C++ , 这会创建一个 buffer overflow ,这is undefined behavior . That's bad .

使用 std::vector相反:

void record(string name, string phoneNum){
    std::vector<std::string> Name;
    std::vector<std::string> PhoneNum;
    Name.push_back(name);
    PhoneNum.push_back(phoneNum);

    //...
}


附言您的程序中还有另一个错误。即NamePhoneNum会在每次函数退出时被销毁。如果这是故意的,那很好。如果你想保留一个运行记录列表,那是不好的。您可以使用 static variable解决这个问题:

void record(string name, string phoneNum){
    static std::vector<std::string> Name;
    static std::vector<std::string> PhoneNum;
    //...
}

关于c++ - 为什么它停止并以退出代码 11 结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601909/

相关文章:

c++ - JUCE: slider ,ValueTree 错误

c++ - 读取文本文件和使用二维 vector

r:除了循环似乎别无选择的情况

C++循环直到输入特定字符

java - Java 中的 While 循环怪异

javascript - 使用 HTTP 请求关闭 for 循环

c++ - 连接功能

c++ - 为什么C++ STL字符串的find函数有时出错有时正确?

c++ - std::move 与 std::auto_ptr 相比?

javascript - 单击按钮时如何生成一组数组而不消失第一个数组?