异常在Visual Studio 2017中使用stoi()无法使用

时间:2017-05-09 20:02:06

标签: c++ visual-studio-2017

我在Visual Studio 2017中尝试制作的程序中遇到了一些问题。问题似乎源于我使用的' stoi()'功能在' load_pokemon'功能。当我尝试运行程序时出现以下错误:

Unhandled exception at 0x74A2A932 in ConsoleApplication1.exe: Microsoft C++ 
exception: std::invalid_argument at memory location 0x010EE918.

有问题的函数在这里(应该假设Pokemon类与所有成员函数一起完全正常运行,因为我相信我对'stoi&#39;的使用是罪魁祸首):< / p>

void load_pokemon(Pokemon pokemon[]) {

    ifstream input_file;
    string file_name;
    int i = 0;
    string temp;
    int number;

    cout << "\n\nEnter Pokemon Data File Name: ";
    cin >> file_name;

    input_file.open(file_name.c_str());

    while (!input_file.eof()) {

        input_file >> temp;
        pokemon[i].assign_name(temp);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_type_1(temp);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_type_2(temp);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_health_base(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_health_multiplier(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_attack_base(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_attack_multiplier(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_s_attack_base(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_s_attack_multiplier(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_defence_base(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_defence_multiplier(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_s_defence_base(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_s_defence_multiplier(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_speed_base(number);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_speed_multiplier(number);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_1(temp);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_1_effect(temp);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_move_1_max_pp(number);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_2(temp);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_2_effect(temp);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_move_2_max_pp(number);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_3(temp);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_3_effect(temp);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_move_3_max_pp(number);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_4(temp);
        temp.clear();

        input_file >> temp;
        pokemon[i].assign_move_4_effect(temp);
        temp.clear();

        input_file >> temp;
        number = stoi(temp);
        pokemon[i].assign_move_4_max_pp(number);
        temp.clear();

        i++;

    }

    return;

}

测试读入文件:

Bulbasaur grass poison 3 2 4 2 5 3 4 2 5 3 3 2 Tackle physical 5 Vine_Whip 
physical 3 Synthesis heal 2 Poison_Powder poison 2

Ivysaur grass poison 4 2 5 2 6 3 5 2 6 3 4 2 Double-Edge physical 5 
Vine_Whip physical 3 Synthesis heal 2 Poison_Powder poison 2

1 个答案:

答案 0 :(得分:1)

如果无法执行转换stoi函数将抛出异常。

我建议您使用调试器并检查字符串变量的内容。

此外,您应该添加代码来捕获异常并处理它。

您可以随时尝试以下代码:

int health_modifier;
input_file >> health_modifier;
pokemon[i].assign_health_modifier(health_modifier);

如果读取失败,流将不会处于good状态。因此,请在阅读后检查input_file的状态(对于所有读取都是个好主意)。