Results 1 to 4 of 4
  1. #1
    P0SEID0N's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    KFC
    Posts
    318
    Reputation
    10
    Thanks
    24
    My Mood
    Lurking

    Learn how to code C++ || Day 4 - Input and Modifying Variables

    Lesson #4 - Input and Modifying Variables

    In my last tutorial you learned about variables and how to output them. But what happens if we want the user of the program to tell us what numbers or letters our variable is going to hold? This is where input comes in handy.

    The format for input is very much the same as the format for output.
    Instead of cout we use cin (console out console in pretty simple really)

    Lets say we have the integer variable dog and we want the user to input what the value of dog is going to be. We would do that like this:
    [PHP]
    cin >> dog;
    [/PHP]
    Notice the arrows, instead of pointing left point right. If you are having trouble remembering which direction is in and which direction is out just make up something to help you. The one i used was, the right arrow points at the door which leads out of the room, the left arrow points to inside the room.

    Modifying Variables

    Lets say hypothetically the value of dog is 3. What happens if you want to change the value of dog to something else6, AFTER it has been declared, without having the user to input it.
    All you have to do is
    [PHP]
    dog = 6;
    [/PHP]
    Now lets say you have the variables, cat, dog and mouse. You wish to make mouse equal cat plus dog. All you have to do is
    [PHP]
    cat + dog = mouse;
    [/PHP]
    This works the same when outputting. If you wish to output cat + dog then
    [PHP]
    cout << cat + dog;
    [/PHP]
    The symbols for operations are:
    + Equals Plus
    - Equals Minus
    * Equals Times
    / Equals Divide


    Now lets use all this in a program.

    Code:
    #include <iostream>
    using namespace std;
    int number1;
    int number2;
    int main()
    {
    cout << "please input number one" << endl;
    cin >> number1;
    cout << "please input number two" << endl;
    cin >> number2;
    cout << "number one plus number two is " << number1 + number2 << endl;
    cout << "number one minus number two is " << number1 - number2 << endl;
    cout << "number one times number two is " << number1 * number2 << endl;
    cout << "number one divided by number two is " << number1 / number2 << endl;
    return 0;
    }
    Credits:
    Me :]

  2. #2
    killashoota1's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    37
    Reputation
    10
    Thanks
    4
    It seems a little short. It could have more information in it. I just wouldnt want it to take like 2 months to learn something that i could learn in about 2-3 weeks somewhere else. But good tuts. Thats the only reason I wouldnt say i absolutely love it

  3. #3
    P0SEID0N's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    KFC
    Posts
    318
    Reputation
    10
    Thanks
    24
    My Mood
    Lurking
    Yea this one is pretty short, its kind of just doing the fine points/recap of what they have already learnt. As some people were getting confused I didn't add all the modulo and stuff in..

  4. #4
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Quote Originally Posted by killashoota1 View Post
    It seems a little short. It could have more information in it. I just wouldnt want it to take like 2 months to learn something that i could learn in about 2-3 weeks somewhere else. But good tuts. Thats the only reason I wouldnt say i absolutely love it
    1. Buy a book
    2. cplusplus.com
    3. sticky: https://www.mpgh.net/forum/31-c-c/481...ter-guide.html

    All good ways to get started. I like P0SIEDAN's tuts too, but for me nothing beats a book, but learn whichever way you want to. If you want to learn to code learn to code for other reason's then hacking, cuz its a long process, and u won't go through with it unless ur tunnel-visioned like myself. xD

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

Similar Threads

  1. Learn how to code C++ || Day 3 - Variables and Date Types
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 8
    Last Post: 04-16-2010, 09:26 PM
  2. Learn how to code C++ || Day 3 - Variables and Date Types
    By P0SEID0N in forum C++/C Programming
    Replies: 5
    Last Post: 04-16-2010, 02:51 AM
  3. Learn how to code C++ || Day 4 - Input and Modifying Variables
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 0
    Last Post: 04-15-2010, 08:49 PM
  4. Learn how to code C++ || Day 1 - Compiler
    By P0SEID0N in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 14
    Last Post: 04-15-2010, 02:42 AM
  5. Learn how to code C++ || Day 1 - Compiler
    By P0SEID0N in forum C++/C Programming
    Replies: 5
    Last Post: 04-13-2010, 06:21 PM