Reputation: 11
I am a new programmer. I get a warning when I run my program in an online editor; the warning is
'number' is used uninitialized in this function [-wuninitialized]".
We used eclipse, which it does not show me this error. But in both I get a number when I run the program.
My code:
double number;
cout << "enter a number: " << number;
cin >> number;
I get this when I run it:
enter a number: 0 or 2.122e-314
but this 0 or 2.12... does not affect the rest of the program!
Upvotes: 1
Views: 129
Reputation: 86
Your code is:
double number;
cout << "enter a number: " << number;
cin >> number;
1.First you declared a variable number
.
2.After that when you use cout<<number
it will print the value which is variable holding.
here the number
is just declared.
the number
is holding nothing so it will print some random value.
3.first you have to put some value into a variable then you can print. so corrected code will be:
double number;
cin >> number;
cout << "number you entered : " << number;
Upvotes: 0
Reputation: 206627
You are writing an uninitialized variable to std::cout
in the line
cout << "enter a number: " << number;
That explains the warning and the output. You need to change it to:
cout << "enter a number: ";
After you read the number, you can use:
cout << "The number you entered: " << number;
double number;
cout << "enter a number: ";
cin >> number;
cout << "The number you entered: " << number;
Upvotes: 2