[C++]Taking In User Input,
Programming
|
DoxCoding.com
Ok so i now hope you have a small understanding how the Hello World Program works.
So you know how to print out output, but do you know how to take in user input. Didnt think so....
Well here i will teach you a little on how to do so.
So lets start.
The simple hello world was.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World!";
return 0;
}
So lets move on from this basic application and take in some user input.
First we need to give out some output to ask for the input.
so lets begin.
Lets go for it.
#include <iostream>
using namespace std;
int main()
{
}
Ok now. We have all the parts we need. (Most of the programs in the rest of articles (if i do anymore) will be using this simple layout to begin. So lets give the output.
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter a number";
}
Ok we have said Enter a number. Simple isnt it. But it gets a bit more tricky now. We bring in something new. cin>>. Its quite simple but can get difficult to understand. Add in that after the cout<<"Enter a number"; line. (Underneath).
So know it should look like this.
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter a number";
cin>>
}
We cannot do anything yet until we declare a variable.
Name Description Size* Range*
char Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255
short int (short) Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to
4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to
4294967295
Ok so this is just a few variable with thier descriptions etc.
So back on track of having to declare the variable. Which one would you pick? For a number.
Correct. int
So what are you waiting for. Put it in. After the first {
So it should now look like this.
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a number";
cin>>
}
Easy isnt it. Well its even easier know. The very last part. Just whatever you put after int whether it be an 'a, b, c, d' etc it doesnt matter. Whatever is after the int put after the cin>> that is now taking IN the user input.
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a number";
cin>>x;
}
TIP: NEVER FORGET THE SEMICOLONS (;) AFTER EACH LINE!
Ok but i bet you are thinking what it the point in that. It does nothing well. That is where you are wrong. We will make it now print out the user input number.
Once again using cout<<"";
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter a number";
cin>>x;
cout<<x;
}
This time can you tell that we dont have to put any " " (quotations) around the x. Why is this? This is because you are not outputing a line that you have chosen. You are instead outputting the user input. You still with me? ANY variable / string / ay you dont need to put any quotations around.
Don't worry about strings and ays yet. You don't need them as you are still a learning newbie. =).
So that is basicly it really.
If anyone has any quesitons about user input and using variable please DON'T be afraid to contact me.
Well here is something for you to go away with. A little bug in this. Try and work it out. There is no more than 3. But i will not say how many there actually are.
#include <iostream>
using namespace std;
int main();
{
int x;
cout<<"Enter a number";
cin>>x
}
Ok well there it is.
You have just done your first input and output.
C y on learning!
Stay TUNED!
-[O]
Please login to rate coding articles.
Click here to register a free account with us.
|
|
Comments
|
| Please login to post comments. |
|
|
 |
Categories |
 |
|
|