Tuesday 27 November 2012

Second Program

This is my second program. Again, another very basic "First Year programming course" program. For the first time I am using comments

What does this program do?

It allows you to input two initials as strings, plus a surname, and then prints them out with a welcome message.

To do that it declares three variables. A variable is like a bucket which stores data. Strings are objects in C++ - but that is far in advance of what I need to know for a program like this. There are other concepts I don't really need to know yet like using namespace , and #include . At the moment all I need to do is remember to include these lines.

At this stage I am really just playing around with getting very basic programs to work




    using namespace std;

    // In this program I use comments like this

    // iostream is a standard library containing code to read and write to the console.

    #include <iostream> 
    // this program will read in two initials
    // and the name
    // and welcome you to programming

    // main is the function which C++ always runs first.
    int main()
    {
        // declares three string variables
        string strFirstInitial;
        string strSecondInitial;
        string strSurname;

        // Enter name
        cout << "Enter your First Initial :" ;
        cin >> strFirstInitial;
        cout << endl << "Enter your Second Intitial :";
        cin >> strSecondInitial;
        cout << endl << "Enter your Surname:";
        cin >> strSurname;

        // Output full name buy adding strings together.
        // endl puts a carriage return into the output.
        // strings start with " " then append each initial with a space for formatting.

        cout << endl << endl << "Welcome to programming in C Mr " + strFirstInitial + " " + strSecondInitial + " " + strSurname << endl;
        //end of program
        return 0;
    }

Why use comments?

Comment provide English language information about what you are trying to do with the program at certain points. In this code, every comment starts with // and goes on to the end of the line.

You can also do in line comments like this: /* this is a comment */

In in line comments, everything inside the asterisk is ignored by the compiler.

See you next week for more adventures in very basic programming ;)



No comments:

Post a Comment