URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       The New DS computer forum!!
  HTML https://dscomp.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: C++
       *****************************************************
       #Post#: 290--------------------------------------------------
       Tutorial #5 - Getting user Input.
       By: Hondaman Date: October 10, 2010, 1:12 pm
       ---------------------------------------------------------
       [center]Welcome to Tutorial No.5
       In this tutorial you will learn how to get user input and store
       it in a variable.[/center]
       [font=trebuchet ms]The Tutorial -[/font]
       Up to this point, we have been doing basic things that you could
       do in notepad.
       OutPutting text, and using variabels
       But in this tutorial we can finally get serious and learn proper
       code!
       What we will be doing in this tutorial is
       - Creating a Variable
       - Getting user input and storing it in a variable.
       - Outputting the variable to show the user what they inputed.
       [font=trebuchet ms]Creating the variable and getting user input
       -[/font]
       Part 1 - setting up
       In the past tutorial you seen how to make a variable hold a
       number with
       [code]
       int cheese;
       cheese = 124;
       [/code]
       But in this program we will be using 'char' to hold letters in
       the variable.
       (If you want to hold numbers in the variable use 'int')
       so the program will look like this so far -
       [code]
       #include <iostream>
       using namespace std;
       
       int main()
       {
       char user_In;
       
       system("pause");
       return 0;
       }
       [/code]
       Now we need to ask the user a question.
       In this tutorial I will ask the user 'Please enter your favorite
       word: '
       this is simple text output. If you can't remember how to do this
       or what it means look at my previous tutorials,
       [code]
       #include <iostream>
       using namespace std;
       
       int main()
       {
       char user_In;
       cout << "Please enter your favorite word: " << endl;
       
       system ("pause");
       return 0;
       }
       [/code]
       Part 2 - Getting user input
       Getting user input is like outputting text.
       It's relativly similar but with a few differences.
       To output you had to type 'cout' for compiler output, now we are
       going to type 'cin' for compiler input.
       and after the 'cin' you have the extraction operator '>>'
       the extraction operator is just like the insertion operator '<<'
       but backwards.
       and then we need to store it in a variable so we need to type
       'user_In;'
       this means the code will look like this.
       [code]
       #include <iostream>
       using namespace std;
       
       int main()
       {
       char user_In;
       cout << "Please enter your favorite word: " << endl;
       cin >> user_In;
       
       system ("pause");
       return 0;
       }
       [/code]
       You could run the code like this but all you would get is
       [code]
       Please enter your favorite word:
       Awesome
       [/code]
       [font=trebuchet ms]Making the program more user friendly
       -[/font]
       Most people would like to see a program reply to them or atleast
       give feedback.
       So think of this as a simple game.
       (By the way, the next tutorial (#6) will be making your first
       game.)
       simply inputing a word and getting a response doesn't sound like
       much fun, but everyone must start out simple.
       for the output
       all you need to do is , type the output, output the content of
       the variable, and clean up the code.
       this can all be done in one line.
       [code]
       cout << "So your favorite word is " << user_In << "? Nice... "
       << endl;
       [/code]
       and thats it.
       so the full script looks like this.
       [code]
       #include <iostream>
       using namespace std;
       
       int main()
       {
       char user_In;
       cout << "Please enter your favorite word: " << endl;
       cin >> user_In;
       cout << "So your favorite word is " << user_In << "? Nice... "
       endl;
       system ("pause");
       return 0;
       }
       [/code]
       Thanks for reading this tutorial.
       If you see any problems with this tutorial, Please PM me.
       Hondaman.
       [center]<< Previous
  HTML http://www.dscompforums.tk/c/tutorial-4-intro-to-variables/msg289/#msg289<br
       />  |   Next >>
  HTML http://www.dscompforums.tk/c/tutorial-6-your-first-proper-game-lost-fortune-part-1/[/center]
       *****************************************************