-4

My problem is that when I enter a value in my C++ program (via cin) it gives me a debug error and says "Run-time Check Failure #3 - the variable 'result' is being used without being initialized." 'result' is an int, and is initialized with a 1, but still gives me this, unlike other peoples' questions.

int multiplication(int x, int y, int result)
{
    result = 1;
    result = x * y;
    return result;
}

int main() //Declares the "main" function, which is mandatory.
{
    int result = 0;
    char operation;
    int x;
    int y;
    std::cout << "Enter your first number: ";
    std::cin >> x;
    std::cout << "Enter your second number: ";
    std::cin >> y;
    std::cout << "Enter your operation (+, -, *, /): ";
    std::cin >> operation;
    if (operation == '*');
    {
        int result = multiplication(x, y, result);
    }

    std::cout << "The answer is: " << result << std::endl;
    return 0; //Ends the process with 0x0.
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203

2 Answers2

3

You have two different result variables.

At the top of main there is one

int result = 0;

which is initialized.

But inside the if-statement the line

int result = multiplication(x, y, result);

creates a new variable, also named result and then passes it to the function. Obviously the function call must be performed before the variable is assigned the value returned from the function.

Probably you didn't want this new variable, but use the one declared at the top of the function. You do that by making it an assignment instead of a new declaration:

if (operation == '*');
{
    result = multiplication(x, y, result);
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • This would imply that the value of `result` gets used in `multiplication()`, which seems rather odd... (Or it could be that whatever runtime checker they have is detecting calling by value without assigning a value?) – Ken Y-N Dec 08 '17 at 02:01
  • @Ken - I didn't copy all the code from the pastebin, but have added the `multiplication` now (which is a bit odd, but valid). The debug runtime is complaining that `result` is passed by value before it *has* a value. – Bo Persson Dec 08 '17 at 02:04
  • Thanks - last time I went to PasteBin very NSFW ads got past AdBlock, so I've blacklisted it on my work computer. Is passing an uninitialised variable undefined behaviour? [This answer says yes](https://stackoverflow.com/a/25074258/1270789), taking the *lvalue* is UB. – Ken Y-N Dec 08 '17 at 02:23
  • 1
    @Ken - Yes, and no. I think in C++17 it might be implementation defined what happens - if all possible bit patterns are valid it could be ok. In older standards it is undefined, but works in practice on a PC (unless the debugger catches it :-). – Bo Persson Dec 08 '17 at 02:29
0

Two things.

  1. You are using two different results.

    int result = 0;
    int result = multiplication(x, y, result);
    

    You need to change to:

    result = multiplication(x, y, result);
    
  2. Why do you need to pass result by parameter? You can simply do this:

    int multiplication(int x, int y) { return x * y; }
    

    In this case, inside the main, change by this:

    result = multiplication(x, y);
    
thatlittlegit
  • 69
  • 1
  • 8
jcrv
  • 647
  • 7
  • 18