-4

I have this problem with my visual studio 2015. The error occurs in the 15th line. Suddenly is says that I hadn't initialize the variable, I don't know how to initialize them.

// Average.cpp. : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main()
{
    int number1, number2, number3;
    double average;
    cout << "Enter three integers and I will display the average" << endl;
    cin >> number1, number2, number3;
    average = (number1 + number2 + number3) / 3.0;
    cout << "The average is" << average << endl;

    return 0;
}
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Jose Puebla
  • 406
  • 1
  • 4
  • 9
  • 4
    The `,` operator does not do what you think! – drescherjm Aug 24 '16 at 19:12
  • 1
    The problem is in the line before: `cin >> number1, number2, number3;`. `number2` and `number3` are not initialized, because they are parsed as separate statements. Use `cin >> number1 >> number2 >> number3;` instead. Voting to close as simple typo. – owacoder Aug 24 '16 at 19:13
  • 1
    Initialising a variable: `int x = 0;`. Initialising two: `int x = 0; int y = 0;` (better) or `int x = 0, y = 0;` (more prone to error and less obvious). This generalises in the obvious way to more than two variables. (This is covered in the introduction to variables in the book you should invest in.) – molbdnilo Aug 24 '16 at 19:18

3 Answers3

3

This line does not do what you expect it to do:

cin >> number1, number2, number3;

You probably wanted to read 3 numbers from cin, this is done the following way:

cin >> number1 >> number2 >> number3;

In the current form it's evaluated as

(cin >> number1), number2, number3;

The result of this expression is number3, which is not initialized in your program. number2 is part of the expression, also not initialized, so compiler warns about it too, though the value is discarded.

Paul
  • 13,042
  • 3
  • 41
  • 59
1

The comma operator separates expressions in C++ and joins them into a bigger expression, so the statement cin >> number1, number2, number3 would read the expression number1, number2, number3 from cin is equivalent to (cin >> number1), number2, number3 which does not make much sense, and is certainly not what you intended to do.

Try cin >> number1 >> number2 >> number3 instead.

This is one of those weird "features" of C++ that the language would have been much better off without.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • It doesn't necessarily *ignore* the rest. In fact, it doesn't, as it results in a error. `number2` and `number3` will get evaluated, and since they are not initialized, you'll get a compiler error. – Rakete1111 Aug 24 '16 at 19:23
  • @Rakete1111 It is not clear to me from the original post, but are you sure this is an error and not a warning? – Mike Nakis Aug 24 '16 at 19:25
  • VS2015 reports an error, I don't know about other compilers – Rakete1111 Aug 24 '16 at 19:26
  • @Rakete1111 OK, I will amend my answer. – Mike Nakis Aug 24 '16 at 19:26
  • GCC and Clang report warnings, so I guess MSCV is wrong again :) – Rakete1111 Aug 24 '16 at 19:27
  • @Rakete1111 nice. C-:= anyhow, I have already removed that part from my answer. – Mike Nakis Aug 24 '16 at 19:28
  • @Rakete1111 It's not an error, it's a warning https://msdn.microsoft.com/en-us/library/axhfhh6x.aspx You may have enabled "Treat warnings as errors" if you are getting an error here. – Paul Aug 24 '16 at 19:34
  • @Paul I haven't, but SDL checks was enabled, which treats specific warnings, like C4700, as errors. Thanks :) – Rakete1111 Aug 24 '16 at 19:37
  • No compiler is wrong. Evaluating an uninitialized variable is undefined behaviour, which means *anything* is allowed by the standard. – Martin Bonner supports Monica Aug 24 '16 at 19:49
  • @MartinBonner Could you please quote the standard where it says that accessing uninitialized variable is undefined behaviour? – Paul Aug 24 '16 at 21:38
  • @Paul: This answer refers to C rather than C++ http://stackoverflow.com/a/6725981/771073, but the standards are very close here. The point is that some behaviour is undefined because of an absence of anything defining the behaviour. – Martin Bonner supports Monica Aug 25 '16 at 05:27
  • @MartinBonner There is undefined behaviour and unspecified behaviour. *[1.3.25] unspecified behavior - behavior, for a well-formed program construct and correct data, that depends on the implementation. [1.3.26] well-formed program - C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule (3.2).* The program where the int variable is not initialized is still well-formed program. There is no undefined behaviour in reading it. – Paul Aug 25 '16 at 05:40
  • I am pretty sure you are wrong; reading an uninitialized variable is not unspecified behaviour, it is undefined. I will comment again if I can find the cite. – Martin Bonner supports Monica Aug 25 '16 at 07:22
  • @MartinBonner You are correct, I take my words back. According to [dcl.init]/12 *If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.18). If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases:* (all exceptions are for narrow character types). – Paul Aug 25 '16 at 16:04
  • I still wonder why the downvote. – Mike Nakis Aug 25 '16 at 18:56
  • @MikeNakis Maybe because of phrasing *would read the expression number1, number2, number3 from cin*. Comma operator has the lowest precedence, so `cin >> number1, number2, number3` is not `cin >> (number1, number2, number3)` but `(cin >> number1), number2, number3`. – Paul Aug 25 '16 at 20:51
0

As mentioned, this should work:

#include <iostream>

using namespace std;

int main()
{
    // ...
    cin >> number1 >> number2 >> number3; // here is the fix

    // ...
    cout << "The average is" << average << endl; // same as you did here

    return 0;
}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104