+ 1
Why does this come out to be what it is? I don't understand how this is working. Help please.
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++.
+ 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.
+ 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.....
+ 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
+ 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.
+ 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.
+ 3
int a = 0;
cout << a;
+ 3
but then you are not getting user input......
+ 3
yea. I can see it
+ 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
+ 3
Welcome!
+ 2
Why are you interested in 0?!
+ 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
+ 2
@Jay,
I noticed.
Thanks though!
+ 1
thank you but i dont understand what the larger number thats in the output represents. and what can this be used for? @jay
+ 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
+ 1
Why are you interested in 0?!
+ 1
ahhhh i understand now i wasnt inputting int as int a=0 i was simply putting int a;
+ 1
thank you for all the help. ive always ignored that box until now.