variablesmaxcin

uninitialized local variable with cin use


I am working on this code (in c++) and I finished but i have 2 errors on line 19 when I use them in for loops about variables y and m, saying that they are uninitialized local variables. I don't see how this is possible because I declared them at the beginning as int and their value is assigned when the user inputs in cin.

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <vector>

using namespace std;

int main()
{
    int a, b, n, l = 0;

    cin >> a, b, n;

    for (int i = 0; i < 20; i++)
    {
        for (int j = 0; j < 20; j++)
        {
            if (l < (i*a + j*b) && (i*a + j*b) <= n)
                l = i*a + j*b;
        }
    }

    cout << l;

    return 0;
}

Solution

  • I'm not in a position to test this, but Multiple inputs on one line suggests that your syntax should be

    cin >> a >> b >> c;
    

    Regardless, I think the compiler is suggesting that assignment to all variables isn't guaranteed by cin so without explicit initialisation when they're declared you're assuming too much.