Home > Software engineering >  C program ends prematurely
C program ends prematurely

Time:09-17

I am really new to programming and c and am experiencing a problem as going through some exercises here:

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;
    }

So I have this loop, the problem is that, after 1 cycle of input, once into temp and then into temp2, the program ends, before I equates 2, instead of going for another cycle of input. Also, I know that a for loop would be better here, it is just that this is only a part of the program I was doing and the exercise requested for the program to be done using while loop. I am with quite limited knowledge and would like to ask, if possible, to explain with a bit more detail, just like you would to the biggest moron you've met in your life!

I didn't think that I need to post the whole program, and believed the issue to be just somewhere here, but just in case it is not:

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;
}

CodePudding user response:

Problem is that your program contains buffer overflow issue.

Note that here address sanitizer detects issue in line 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

So basically program terminate since in line numbers holds only one number and you are trying access second one (with index 1).

I do not know how to fix it since your code is to messy to understand what was your aim. Please learn to split your code into multiple small functions ASAP. It will be easier to read correct and understand intent of code author.

  • Related