Coder Profile - Show off your skills, get a coder profile.
 
 
 
[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.
 y on learning!

Stay TUNED!
-[O]


Posted By otoom
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
Page 1 of 1
More Articles By This Author
Quick Threading Tutorial - C++
First look at C
[PHP] How to make a log file.
[VB] Hello World
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
[PHP] - Lets kill the complex IF
Java: Obtaining User Input - Part 1
Basic PortScanner in VB6.0
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
Recently Rated "Programming" Articles
[PHP] - Lets kill the complex IF
Java: Obtaining User Input - Part 1
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (51)
Security (10)
Software Development (5)
Standards (1)
Web Development (15)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
Which is your favorite web language?

ASP / ASP.Net
JSP
PHP
Perl
Python


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
5 Days Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved