C/C++ for Hackers: Part 8 (Getting User Input)

Part 8 (Getting User Input)

C/C++ for Hackers: Part 8 (Getting User Input)

hello again, fellow hackers! sorry for the delay, but i will be rolling out tutorials again! in this tutorial, we will talk about how to get user input and store it in a variable. then we will print out that variable.

Step 1: Fire Up Ubuntu and Open a Text Editor

your first step is of course to fire up Ubuntu and open a text editor. then paste this code in your main.cpp file and compile it. now run it. you'll see that our program asks us to enter a message, then it prints it out on a new line.

BE AWARE! cin can't detect non ascii characters (i'll cover the ascii table later), such as spaces. to us a space in your message, just use a underscore instead.

Explanation Time!

when we read through the code, we can see that we first declare a string variable, called Message. then we prompt the user to enter some text. then we use cin, and command it to store the input it gets in the Message variable. then we print out what is in the message variable.

take note that in cin, the arrows point to the right, opposed to the arrows of cout, which point to the left. but take especially note that the arrows of cin point to the variable, meaning "put the value you get in this variable." and the arrows pointing to the left in cout basically mean "put this value in cout to print out".

take note that cin can be used with any variable, but then you must make sure you don't enter a string when you are looking for an integer!

that is it for now folks! i know, a little bit of a short tutorial, but there isn't much to cin, really. in part 9, we will stop using a text editor and install my favorite IDE, CodeBlocks. (i will talk then about what an IDE is and why i love CodeBlocks so much) and in part 10, we will write our first, basic computer virus!

if you have any questions or problems, feel free to comment below or PM me!

-Phoenix750

1 Comment

example :

#include <iostream>
using namespace std;
int abc;
string def;
int main() {
cout<<"Input Number :";
cin >> abc;
cout<<"Input Texts :";
cin >> def;
cout<<"+--------------------------------------+"<<endl;
cout<<"Numeric : "<<abc<<endl;
cout<<"String : "<<def<<endl;
return 0;
}

view :
Input Number : 123456
Input Texts : Hallo
+--------------------------------------+
Input Number : 12345
Input Texts : Hallo

Share Your Thoughts

  • Hot
  • Latest