+ 1

Why does this come out to be what it is? I don't understand how this is working. Help please.

https://code.sololearn.com/ci7umjQ2g5A0/?ref=app

19th May 2017, 2:56 PM
DawnWeaver
DawnWeaver - avatar
23 odpowiedzi
+ 6
#include <iostream> using namespace std; int main() { int a; //This declares initalises variable a as an integer cout << "2 \n"; //This will print the 'string' 2 to the screen cin >> a; //This prompts the user to input any integer cout << 0; //This will print 0 to the screen return 0; //This checks normal termination of the program } if you add cout << a << endl; it should be able to print the integer value the user inputs. ⚠ It is also good practice to use 'endl' instead of '\n' for a new line in C++.
19th May 2017, 3:26 PM
Tusiime Innocent Boub
Tusiime Innocent Boub - avatar
+ 4
Write it like this to get info from the user int a = 0; cout << " please enter a number"; cin >> a; cout << a; now when you run it in the box that appears enter a number.
19th May 2017, 3:18 PM
jay
jay - avatar
+ 4
you know that box that appears that says It looks like your program needs input. You program is asking for a number. If you dont supply one the variable a is empty and can point any old where. By saying int a = 0 or int a = 42; we tell it that by default the variable a will be that value.....
19th May 2017, 3:25 PM
jay
jay - avatar
+ 3
you need return 0 but that doesnt matter here. it would though if you compiled this as an exe and ran it but everything else seems fine. You enter a number and it shows you the number you entered. what is it you do not understand exactly? i do not understand what you do not understand
19th May 2017, 2:59 PM
jay
jay - avatar
+ 3
which larger number? where? the only number is the ones you enter. try changing return 1 to return 0. return 0 tells the operating system that the program ran fine and there were no errors. maybe this is what you see.
19th May 2017, 3:04 PM
jay
jay - avatar
+ 3
Oooo I see what you are doing wrong now. you didnt enter a value when prompted the 2 is from cout. this is printed by the computer. cin prompts the user for an entry. If you dont enter a number it will be grabbing junk data. You can do nothing with this data. You should initialise the variable a to a default value to prevent it printing garbage you can do this by int a = 0; try that.
19th May 2017, 3:12 PM
jay
jay - avatar
+ 3
int a = 0; cout << a;
19th May 2017, 3:15 PM
jay
jay - avatar
+ 3
but then you are not getting user input......
19th May 2017, 3:17 PM
jay
jay - avatar
+ 3
yea. I can see it
19th May 2017, 3:20 PM
jay
jay - avatar
+ 3
because it is a default value incase the user enters nothing. its stops it from producing the large random value you were complaining about
19th May 2017, 3:22 PM
jay
jay - avatar
+ 3
Welcome!
19th May 2017, 3:28 PM
jay
jay - avatar
+ 2
Why are you interested in 0?!
19th May 2017, 3:21 PM
Tusiime Innocent Boub
Tusiime Innocent Boub - avatar
+ 2
@Tuslime... mmhmmm it started out right. User wasnt entering any data when prompted... so a was spewing junk... lol... they edited it to that... but it sorted now right @Dawn
19th May 2017, 3:31 PM
jay
jay - avatar
+ 2
@Jay, I noticed. Thanks though!
19th May 2017, 11:43 PM
Tusiime Innocent Boub
Tusiime Innocent Boub - avatar
+ 1
thank you but i dont understand what the larger number thats in the output represents. and what can this be used for? @jay
19th May 2017, 3:01 PM
DawnWeaver
DawnWeaver - avatar
+ 1
the output has 2 but then there is a larger number beneath it that i did nothing to have put there. I dont get where this numer comes from. @jay
19th May 2017, 3:08 PM
DawnWeaver
DawnWeaver - avatar
+ 1
Why are you interested in 0?!
19th May 2017, 3:21 PM
Tusiime Innocent Boub
Tusiime Innocent Boub - avatar
+ 1
ahhhh i understand now i wasnt inputting int as int a=0 i was simply putting int a;
19th May 2017, 3:25 PM
DawnWeaver
DawnWeaver - avatar
+ 1
thank you for all the help. ive always ignored that box until now.
19th May 2017, 3:26 PM
DawnWeaver
DawnWeaver - avatar