+ 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

18th Oct 2018, 10:53 AM
Shubham Maurya
Shubham Maurya - avatar
26 odpowiedzi
+ 8
Enter it on another line in the prompt box. Like so: 5 6
19th Oct 2018, 8:23 AM
jay
jay - avatar
+ 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
18th Oct 2018, 12:08 PM
jay
jay - avatar
+ 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?
19th Oct 2018, 8:17 AM
jay
jay - avatar
+ 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;
18th Oct 2018, 11:08 AM
jay
jay - avatar
+ 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.
18th Oct 2018, 11:49 AM
jay
jay - avatar
+ 6
Lol, so true.. Can you perhaps not swear though..
18th Oct 2018, 12:29 PM
jay
jay - avatar
+ 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.
18th Oct 2018, 11:39 AM
jay
jay - avatar
+ 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;
18th Oct 2018, 11:57 AM
jay
jay - avatar
+ 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.
18th Oct 2018, 12:26 PM
Babak
Babak - avatar
+ 5
Hey jay ! ;D Swear?! Who?! Me?!
18th Oct 2018, 12:47 PM
Babak
Babak - avatar
+ 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; }
18th Oct 2018, 12:23 PM
jay
jay - avatar
+ 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.
18th Oct 2018, 4:54 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
please help
18th Oct 2018, 10:57 AM
Shubham Maurya
Shubham Maurya - avatar
+ 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
18th Oct 2018, 12:54 PM
Addanki Bharadwaj
Addanki Bharadwaj - avatar
+ 2
what are u people talking about? I'm beginner learner of level2 and my query is still unsolved !
19th Oct 2018, 7:35 AM
Shubham Maurya
Shubham Maurya - avatar
+ 2
why didn't it click to me
19th Oct 2018, 8:25 AM
Shubham Maurya
Shubham Maurya - avatar
+ 1
use endl before terminating the cout statement
18th Oct 2018, 11:30 AM
Mayank kr
Mayank kr - avatar
+ 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
18th Oct 2018, 11:31 AM
Shubham Maurya
Shubham Maurya - avatar
+ 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'
18th Oct 2018, 11:39 AM
Shubham Maurya
Shubham Maurya - avatar
+ 1
#jay i didn't understand if 2nd variable is uninitialised then how will compiler know its value is 8
18th Oct 2018, 11:42 AM
Shubham Maurya
Shubham Maurya - avatar