为什么停止并以退出代码11结束? [英] Why it stops and finished with exit code 11?

查看:68
本文介绍了为什么停止并以退出代码11结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't know why it stops there and finished with exit code 11. It suppose to run until I give the command.

#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[] = {};和其他类似的东西没有按照您认为的做.它们创建一个空字符串数组.由于变量长度数组不是C ++中的东西,这会创建缓冲区溢出,其中是未定义的行为. 这很糟糕.

That's bad in C++ because string Name[] = {}; and others like it don't do what you think they do. They create an empty array of strings. Since variable length arrays are not a thing in C++, this creates a buffer overflow, which 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);

    //...
}


P.S.您的程序中还有另一个错误.也就是说,每次函数退出时,NamePhoneNum将被销毁.如果打算这样做,那就好了.如果您希望保留记录的运行清单,那就不好了.您可以使用静态变量来解决此问题:


P.S. There is another bug in your program. That is, Name and PhoneNum will get destroyed when the function exits each time. If that is intended, then fine. If you wish to keep a running list of records, that is bad. You can use a static variable to fix this:

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

这篇关于为什么停止并以退出代码11结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆