Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,408,675

C++ Computer Programming [Tutorial #2] Input/Output

Tutorial Name: C++ Computer Programming [Tutorial #2] Input/Output  

Category: PC Tutorials

Submitted By: HTTK

Date Added:

Comments: 2

Views: 4,538

Related Forum: PC Building Forum

Share:

Welcome to my C++ Programming Tutorials!

Part 2: Input/Output




We will start by adding the same library's and namespace along with the main function.
#include <iostream>
using namespace std;

int main(){

}

"iostream" is the input/output library which is exactly what we will be doing.


We are going to make a simple program the ask the user to input a number.
Then the program will output the users input.


We will now have to declare a integer in the main function.
We do this by typing "int" followed by what ever you wanted it to be called.
I am going to make mine simply and name it "userInput"
Try to always make the names make sense as it will help you later on.
Here is what it should look like:
#include <iostream>
using namespace std;

int main(){

int userInput;

}
Notice how made the "I" in input capital and "user" lowercase.
This is called camel case and it is not required but good style!


Now we will use what we know from TUT#1 to output some text using the "cout" command.
We will also add system("pause"); before I forget!
Here is what you should have:
#include <iostream>
using namespace std;

int main(){

int userInput;

cout << "Enter a integer: \n";

system("pause");

}
If you were wondering what "\n" is, it just simply ends the line.
Much like typing enter on a keyboard.


Now we need to add code to allow the user to input a number.
We will be using the code "cin"
This one is fairly simply just do as I have:
#include <iostream>
using namespace std;

int main(){

int userInput;

cout << "Enter a integer: \n";
cin >> userInput;

system("pause");

}
Notice the ">>" we used are different than before.
That is because we are inputting not outputting.
Then we simply state what variable we want the integer to be saved to!


Now we have one last thing to do and that is to output our "userInput"
We will again be using "cout" command but instead of typing out what to input we simply tell the program to output "userInput"
#include <iostream>
using namespace std;

int main(){

int userInput;

cout << "Enter a integer: \n";
cin >> userInput;

cout << "You entered: " << userInput << "\n";

system("pause");
return 0;
}
We had to add the "<<" again after the text to show we are outputting another line of code.
Also I have added "return 0;" which I explain in part 3.
Also ended the line of code for style.


Hope you enjoyed!
Part 3 will be soon!

Ratings

Current rating: 5.27 by 11 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"C++ Computer Programming [Tutorial #2] Input/Output" :: Login/Create an Account :: 2 comments

If you would like to post a comment please signin to your account or register for an account.

RareparrotPosted:

thanks for the tut man trying it now

CyimkingPosted:

Highly suggest you make this a bit more advanced. For example, add in getline, when to use cin.ignore() ,etc... and yes it's important. If a user is asking for a user input during a loop BASED on this tutorial, the code will only ask for an input once and end the loop.