+ 1
why does this code insert '8' in output
#include <iostream> using namespace std; int main() { int a, b; cout << "Enter a number \n"; cin >> a; cout << "Enter another number \n"; cin >> b; cout << a ; cout << b ; return 0; } //it is not working as expected
26 odpowiedzi
+ 8
Enter it on another line in the prompt box. Like so:
5
6
+ 7
If nothing is entered for the second input, it will output 0.. Thus it becomes predictable.
#include <iostream>
using namespace std;
int main()
{
int a =0;
int b = 0;
cout << "enter a number \n";
cin>>a;
cout << "enter a number \n";
cin>>b;
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
If you wanted nothing to be printed to the screen when the user did not enter a value (or entered 0) you could now detect it by using an if statement. You can initialise to any value that the variable can hold. I.e in the code above -1 could easily be substituted for 0.
Then you could add something like:
If b = -1 do nothing else print b
+ 7
What part do you not understand?
That two entries are required in your code?
Or that the 8 is a random value that was stored there by another program?
+ 6
Did you enter an 8, because it should work just fine.
Try separating your outputs at the end of the program.
Example
cout << a << endl;
+ 6
It could be 8 (in this case it is) , it could be 4383, it could be 42 Thus the part of the sentence, "not predictable"
Basically: When you declare but not intialise a variable, the compiler sets space aside for the variable on the stack, but does not set its contents.
The 8 you are seeing when you don't enter anything is what is in this space.
+ 6
Lol, so true.. Can you perhaps not swear though..
+ 5
Ooooo, you are not entering anything at all for the second input.
See:
https://en.m.wikipedia.org/wiki/Uninitialized_variable
Uninitialized variable. In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one.
+ 5
When the program is run the computer will set the appropriate stack space, it could be any free memory area.
Best way to avoid this would just to initialise your variables..
E.g
int a = 0;
+ 5
This (ridiculous) problem is a SoloLearn-specific issue and in particular has been occurred due to the fact that there's no standard prompt mechanism like what is available on a terminal environment which running on a workstation. In case of SoloLearn, you have to be careful about the number of expected inputs since there's no hint or system prompt to pause and advise the user what to do. In an actual machine, however, you almost always get asked for EACH input individually, so that's impossible to mess things up or in your case ending up with undefined behavior as the result of uninitialized variables.
+ 5
Hey jay ! ;D
Swear?! Who?! Me?!
+ 4
Example:
#include <iostream>
using namespace std;
main()
{
int a =-1;
int b = -1;
cout << "enter a number \n";
cin>>a;
cout << "enter a number \n";
cin>>b;
if(a != -1)
cout<<a<<endl;
if(b!=-1)
cout<<b<<endl;
return 0;
}
+ 3
Gently, validating inputs is the programmer's task but...it's okay when you're just starting to expect people to work with--and not break--your code.
Later on in your career though, people will go out of their way to do things your code doesn't expect, for good and not so good reasons (including failing to answer a prompt even though it's right there).
The Internet was built on the trust model in my first sentence, but exposing it to the world made knowing the second one important.
+ 2
please help
+ 2
Technically, the value of an uninitialized non static local variable is Indeterminate
In short it can be anything. Accessing such a uninitialized variable leads to an Undefined Behavior.
C99 section 6.7.8 Initialization:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.
Accessing an unitialized variable is undefined behavior in both C and C++, so reading any value is possible.Sololearn is reading it as 8..So 8 gets printed everytime
+ 2
what are u people talking about? I'm beginner learner of level2 and my query is still unsolved !
+ 2
why didn't it click to me
+ 1
use endl before terminating the cout statement
+ 1
Thank you for cooperation.
but it didn't work
It gives same output differ by just adding a new line for '8' (obviously)
and I ain't entering 8
+ 1
#naini,#mayank_kr,
it demands for input. If i give an input like 2 then it gives output 28. If i give 45 its output becomes 458.
This code is created for give an output what i give input but every time it inserts this bloody '8'
+ 1
#jay
i didn't understand
if 2nd variable is uninitialised then how will compiler know its value is 8