Activity Stream
48,167 MEMBERS
62422 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 2 of 2
  1.     
    #1
    Member
    Website's:
    Pirateview.org Gfxs.org StarkWood.org

    Default C++ Variables & IO [Lesson 2]

    Welcome again, to the second lesson on C++!

    Hope you have read the first lesson, if not, feel free to check it out!
    Code: 
    http://pirateview.org/viewtopic.php?f=26&t=39
    Today, am gonna introduce you to variables and how to do an input or an output with c++. First of all, what is a variable? Suppose, i have a program that ask me to enter my name and then displays a welcome message. After i input my name, i have to store it somewhere. That somewhere could be a variable. A variable is something, which value changes. Another example, lets say i need to take temperature from the atmosphere and enter it in a program several times a day! A variable, "temperature" could be used in this case and its value changes with the change in temperature.

    In C++, we must declare any variable before using it and to do this we write the type of variable we want to use, followed by its name. Lets take a variable, "age", The variable's name is "age" and its values would be numbers(integers). So the type of variable used would be integers.

    Declaring a variable in C++:
    type variableName; //Don't forget the Semicolon.



    After Declaring a variable, we can assign a value to it and use it. To assign a value to the variable, we use the equal("=") sign.
    variable = value;



    Now, you can use this variable to do some processing or you can simply display it! Lets try displaying it!


    And if you compile the code, you will get this


    hey, why the cout is like that? Let me explain! Suppose you only want to display the age, you can write this code:
    cout << age;

    it will only display the value of the variable "age", which is 17!

    What if i want to display several lines and variables together, you could do something like:
    cout << "My age is ";
    cout << age;


    This would display "My age is 17". But there is a shorter way of displaying multiples lines and variables together. This is illustrated below:
    cout << "my age is " << age; //Will display "my age is 17".

    and like that you can continue on:
    cout << "Hello my name is " << myName << " and i am " << age << " years old!"; //Where myName is a variable storing someone's name.

    Note: You can also assign a value to a variable when you declare it.
    type variable = value;



    The result of the above code will be same as the previous code:


    Challenge 1:
    This is a little challenge for you! Write a little program with 2 variables, "oranges" & "apples" and assign the number of oranges and apples you like to eat per week. Finally, display a text, something like:
    "I like to eat 10 Oranges and 5 Apples per week!"

    Of course, you should use the variables you declared as the number of oranges and apples in the text! Goodluck! The answer to this challenge is provided at the end of this tutorial! Don't look, before completing the challenge!

    Characters & Strings
    Till now, we only dealt with numbers, what if we want to store some characters, letters or some string. In C++, characters are dealt in a different way. Suppose i want to store someone name in a variable, of course this time we won't be storing a number but rather a series of characters. We will make use of the char type for declaring our variable.

    char name = 'a'; //Here you will notice that i have put only 1 character and in single quotes. It is because you can only store a single character in a variable and whenever you need to store a single character, you must use single quotes rather than doubles quotes which are used for more than 1 character.

    If you write something like,
    char name = "Alex"; //You will end up with an error and your program won't run! Never forget, variables can store only 1 character!

    You are surely thinking , "how am i gonna store a person name, if i can only store a single character?". Well, we will need to store the name in an array. What is an array now? An array is simply a collection of elements. In this case, our array will be a collection of characters representing a person's name.
    The difference between a variable's name and the name of an array is that when declaring a variable name, you just have to type the name, whereas with an array name, you type the name of the array followed by an open square bracket and a closed square bracket as follows:
    char name[] = "Alex";

    We will get Arrays in details in our next tutorials!

    Lets see this in practice now. Suppose i have to store John's and Marry's name and i need to display their name on the screen.


    The result:


    Another way, you could do this, instead of using an array of characters but by using a variable, is by making use of the string library. Libraries are also called Header files. From now on, we will use the term "header file" to refer to any library. So, we must first include the "string" header file.
    #include <string>

    string name = "alex"; //Variable "name" is declared as string type, you can't use this type unless you have included the string header file!






    Input
    We have not yet seen, how we will get a user to input something! We will use the cin command for that. The cin command is like the cout command, only the difference is that, in the cin command you make use of double greater sign and instead of writing a text, you will write a variable name where the input will be stored.

    Syntax:
    cin >> someVariable; //someVariable will get the value of the input from the user.

    We will now code a little program to ask our user to input his name and we will greet him by displaying a welcome message.



    One thing you may have notice is that i have written a number between the square brackets of the array. This number is the size of our array, meaning how many characters it can hold! Approximately, you can guess how many characters a variable may take and you MUST specify the array size or else you will get an error! Another change i made, is by adding an additional getchar() line. I won't get into the technical details right now, but we will surely discuss that in other posts. What you need to know is that whenever you are taking an input, you must write getchar() 2 times, so as to be able to read your final message. If you wrote getchar() only once, the program would run but you would not see the final welcome message! too bad, huh :?

    I almost forgot, here is the final result:


    Challenge 2:
    Now, the real fun begins! You will need to write a program that will take in the user's name, age, address and phone number and display the information on the screen. Lets see what you come up with this one!

    Further Reading:
    1. Variables names: There are some rules on how you should write a variable name. An incorrect variable name may give you some errors. Variables names may contain alphabets, numbers or underscores.

    Wrong variable names:
    int Number-1;
    char @myname;
    int the number of apples;

    Good variable names:
    int number1;
    char the_number_of_apples;

    Note: if you want to write a variable name consisting of several word, separate each word by an underscore.

    2. New line: If you want to write something in a new line, you can write "\n" in your cout command.

    Example:
    cout << "This is my first line! \n This is a second line!";

    Display:
    This is my first line!
    This is a second line!

    Answers to challenges:
    Challenge 1:




    Challenge 2:




    Download Source code:
    Code: 
    http:///node.pirateview.org/1146/variables.zip
    Well, that's all for now!

    Thanks a lot for reading this tutorial, i hope you did so and read it all! If you are having any problems with the code or want to add up, feel free to reply this thread or get us @ pirateview.org.

    Am taking a lot of time and effort to write these tutorials all by myself! I try to make it as simple as possible, for you people. So the least i can ask you, is to register on our site, Pirateview.org and to suport us by replying, posting feedback and participating in the board! We also need your contribution, if you feel, you can improve this tutorial!

    Thanks and have a nice day!
    NucleA Reviewed by NucleA on . C++ Variables & IO [Lesson 2] Welcome again, to the second lesson on C++! :) Hope you have read the first lesson, if not, feel free to check it out! http://pirateview.org/viewtopic.php?f=26&t=39 Today, am gonna introduce you to variables and how to do an input or an output with c++. First of all, what is a variable? Suppose, i have a program that ask me to enter my name and then displays a welcome message. After i input my name, i have to store it somewhere. That somewhere could be a variable. A variable is Rating: 5

  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    scrls.co.uk
    thanks for these, keep posting as i am thinking about learning C++

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. SSH Command Variables
    By Maverick in forum Server Management
    Replies: 1
    Last Post: 11th Jan 2012, 05:40 AM
  2. C++ Basics [Lesson 1]
    By NucleA in forum Web Development Area
    Replies: 7
    Last Post: 3rd Jan 2011, 07:15 AM
  3. [c#] variables, ifs, simple math and drawing objects
    By jayfella in forum Web Development Area
    Replies: 23
    Last Post: 18th Jun 2010, 04:52 PM
  4. Swap photo/avatar variables
    By Golden Falcon in forum IP.Board
    Replies: 7
    Last Post: 12th Feb 2010, 06:18 PM
  5. Optimizing the mysqld variables
    By Lease in forum Technical and Security Tutorials
    Replies: 0
    Last Post: 14th Jan 2008, 05:30 AM

Tags for this Thread

BE SOCIAL