You are viewing our Forum Archives. To view or take place in current topics click here.
C++ Computer Programming [Tutorial #2] Input/Output
Posted:
C++ Computer Programming [Tutorial #2] Input/OutputPosted:
Status: Offline
Joined: Jul 25, 201410Year Member
Posts: 2,496
Reputation Power: 313
Status: Offline
Joined: Jul 25, 201410Year Member
Posts: 2,496
Reputation Power: 313
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!
The following 1 user thanked HTTK for this useful post:
Dusknoir (03-29-2015)
#2. Posted:
Status: Offline
Joined: Apr 23, 201212Year Member
Posts: 1,560
Reputation Power: 92
Status: Offline
Joined: Apr 23, 201212Year Member
Posts: 1,560
Reputation Power: 92
Perfect, I have been trying to get some knowledge of C++ to see if I can play around with CS:GO. Not aimbot, more of a less cheat worthy script.
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Dec 30, 201212Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201212Year Member
Posts: 3,778
Reputation Power: 3016
You can also remove the "int" and rename it to "void" to allow it to run without the "return 0;" as I see it as messy. But that's just my opinion.
- 0useful
- 1not useful
#4. Posted:
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Please stop teaching these bad habits.
1) Use return 0; in your main method (on all examples).
2) Don't use system("pause"); - windows specific. Use cin.ignore().get();
3) Why use "\n" when you can use endl ?
1) Use return 0; in your main method (on all examples).
2) Don't use system("pause"); - windows specific. Use cin.ignore().get();
3) Why use "\n" when you can use endl ?
- 1useful
- 1not useful
#5. Posted:
Status: Offline
Joined: Jul 25, 201410Year Member
Posts: 2,496
Reputation Power: 313
Status: Offline
Joined: Jul 25, 201410Year Member
Posts: 2,496
Reputation Power: 313
ObscureCoder wrote Please stop teaching these bad habits.
1) Use return 0; in your main method (on all examples).
2) Don't use system("pause"); - windows specific. Use cin.ignore().get();
3) Why use "\n" when you can use endl ?
Please stop commenting. These are short tutorials and aren't meant to be for professional use.
I know c++ well and I was taught this way and later was taught the proper way.
This tutorial assumes that the person doesn't know any programming language.
Therefore I am making it simple as possible.
- 0useful
- 1not useful
#6. Posted:
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Seize_The_Day wroteObscureCoder wrote Please stop teaching these bad habits.
1) Use return 0; in your main method (on all examples).
2) Don't use system("pause"); - windows specific. Use cin.ignore().get();
3) Why use "\n" when you can use endl ?
Please stop commenting. These are short tutorials and aren't meant to be for professional use.
I know c++ well and I was taught this way and later was taught the proper way.
This tutorial assumes that the person doesn't know any programming language.
Therefore I am making it simple as possible.
If you have the audacity to teach, you should have knowledge to teach it correctly.
I'm sure you're an expert at C++ (jking I think you've barely touched the language).
Simple != Wrong.
- 1useful
- 1not useful
#7. Posted:
Status: Offline
Joined: Oct 24, 201113Year Member
Posts: 12,654
Reputation Power: 718
Status: Offline
Joined: Oct 24, 201113Year Member
Posts: 12,654
Reputation Power: 718
Seize_The_Day wroteObscureCoder wrote Please stop teaching these bad habits.
1) Use return 0; in your main method (on all examples).
2) Don't use system("pause"); - windows specific. Use cin.ignore().get();
3) Why use "\n" when you can use endl ?
Please stop commenting. These are short tutorials and aren't meant to be for professional use.
I know c++ well and I was taught this way and later was taught the proper way.
This tutorial assumes that the person doesn't know any programming language.
Therefore I am making it simple as possible.
Although I do not know a lot about programming in this language, you really DO need to write tutorials in the correct fashion instead of teaching users bad habits that they will later need to break. So what if the proper way is a little harder? You will learn much more easily once you have the proper method down.
- 1useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.