C++程序过早结束

C++ program ends prematurely

我真的是编程和 C++ 的新手,在这里做一些练习时遇到了问题:

int i = 0;
vector<double>numbers;
        double temp;
      
    vector<string>unit;
        string temp2;

 while ( i <= 2)
    {
       
        cin >> temp;
        numbers.push_back(temp);

        
        cin >> temp2;
        unit.push_back(temp2);

        ++i;
    }

所以我有这个循环,问题是,在 1 个输入循环之后,一次进入 temp,然后进入 temp2,程序结束,在我等于 2 之前,而不是进行另一个输入循环。 另外,我知道这里使用 for 循环会更好,只是这只是我正在执行的程序的一部分,并且练习要求使用 while 循环来完成程序。 我的知识非常有限,如果可能的话,我想请你解释得更详细一点,就像你对你一生中遇到的最大的白痴一样!

我认为我不需要 post 整个程序,并且认为问题就出在这里,但以防万一:

int main()

{
     vector<double>numbers;
        double temp;
        int i = 0;
    vector<string>unit;
        string temp2;


    cout << "This program will take an input of 2 units, convert them both in meters and compare them\n";
    while ( i <= 2)
    {
        cout << "\n Please enter value " << i + 1 << "\n";
        cin >> temp;
        numbers.push_back(temp);

        cout << "\n Please choose the units of your value - meters/m; centimeters/cm; feet/ft; inches/in.\n";
        cin >> temp2;
        unit.push_back(temp2);

        ++i;
        cout << i;

        if (i == 1 && unit[0] == "m" || unit[0] == "meters")                        //Prints value 1 and prints its respective unit, then converts it into m
        cout << "Your first value is: " << numbers[0] << unit[0] << "\n";
        else if (i == 1 && unit[0] == "in" || unit[0] == "inches"){
            cout << "Your first value is: " << numbers[0] << unit[0] << ". This is equal to " << numbers[0] / 254 << "meters" << "\n";
            numbers[0] /= 254;
        }else if (i == 1 && unit[0] == "cm" || unit[0] == "centimeters"){
        cout << "Your first value is: " << numbers[0] << unit[0] << ". This is equal to " << numbers[0] / 100 << "meters" << "\n";
        numbers[0] /= 100;
        }else if (i == 1 && unit[0] == "ft" || unit[0] == "feet"){
        cout << "Your first value is: " << numbers[0] << unit[0] << ". This is equal to " << numbers[0] * 0.3048 << "meters" << "\n";
        numbers[0] *= 0.3048;
    }else
        cout << "Sorry, I do not recognise that unit.\n";



        if (i == 2 && unit[1] == "m" || unit[1] == "meters")                        //Prints value 2 and prints its respective unit, then converts it into m
        cout << "Your second value is: " << numbers[1] << unit[1] << "\n";
        else if (i == 2 && unit[1] == "in" || unit[1] == "inches"){
            cout << "Your second value is: " << numbers[1] << unit[1] << ". This is equal to " << numbers[1] / 254 << "meters" << "\n";
            numbers[1] /= 254;
        }
        else if (i == 2 && (unit[1] == "cm")  || unit[1] == "centimeters"){
        cout << "Your second value is: " << numbers[1] << unit[1] << ". This is equal to " << numbers[0] / 100 << "meters" << "\n";
        numbers[1] /= 100;
        }
        else if (i == 2 && unit[1] == "ft" || unit[1] == "feet"){
        cout << "Your second value is: " << numbers[1] << unit[1] << ". This is equal to " << numbers[0] * 0.3048 << "meters" << "\n";
         numbers[1] *= 0.3048;
        }else
        cout << "Sorry, I do not recognise that unit.\n";

        cout << "Therefore\nValue 1 = " << numbers[0] << " meters\n" << "Value 2 = " << numbers[1] << " meters\n";

        cout << "Hence: \n";
        if ( numbers[0] > numbers [1] && i == 2)
            cout << "Your second value, " << numbers[1] << " m is the smaller value"  << "\tThe larger value is: " << numbers[0] << " m\n";

        else if (numbers[0] < numbers[1] && i == 2)
            cout << "Your first value, " << numbers[0] << " m is the smaller value" << "\tThe larger value is: " << numbers[1] << " m\n";

        else if (numbers[0] == numbers[1] && i == 2)
            cout << numbers[0] << " m and " << numbers[1] << " m are equal";


        if (numbers[0] > numbers [1] && i == 2)
        {
            if ((numbers[0] - 0.01) < numbers[1])
                cout << numbers[0] << " m and " << numbers[1] << " m are almost equal.";
        }else if (numbers[0] < numbers[1] && i == 2)
        {
            if ((numbers[0] + 0.01) > numbers[1])                      
            cout << numbers[0] << " m and " << numbers[1] << " m are almost equal.";
        }





    }



    return 0;
}

问题是您的程序包含缓冲区溢出问题。

请注意 here address sanitizer 检测行 74 中的问题:

==1==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000018 at pc 0x000000406843 bp 0x7fffc3ddb350 sp 0x7fffc3ddb348
READ of size 8 at 0x602000000018 thread T0
    #0 0x406842 in main /app/example.cpp:74

所以基本上程序终止,因为行 numbers 只包含一个数字,而您正在尝试访问第二个数字(索引为 1)。

我不知道如何修复它,因为你的代码太乱了,无法理解你的目的。请尽快学会将代码拆分为多个小函数。更容易正确阅读和理解代码作者的意图。