Tutorials Navigation
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,537
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.
"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:
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:
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:
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"
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!
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;
}
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");
}
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");
}
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;
}
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
Comments
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.
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,491)
- 02. How to: Matrix Numbers | Batch File(1,902)
- 03. How to Password Protect Files on Windows(857)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,705)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,132)
- 06. How to embed an image on TheTechGame(3,096)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(12,980)
- 08. Host bot lobbies! Full Tutorial!(11,240)
- 09. Unban yourself [Plutonium BO2](14,239)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,385)
- 11. Best Crosshair Settings for Valorant(6,525)
- 12. Othercide The Surgeon Boss Guide(2,537)
- 13. Othercide Remembrances Unlock Guide(4,459)
- 14. Othercide Beginners Tips and Tricks(2,707)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,842)
"C++ Computer Programming [Tutorial #2] Input/Output" :: Login/Create an Account :: 2 comments