Wednesday, December 1, 2010

csci135c1.cpp

Since this is the first tutorial, and I am still trying to figure out my way of how to do this. Let's do a easy example, the well know "Hello World" example. This program will display a message to the terminal.
/*
 *  csci135c1.cpp
 *  The classical program that display a message to the screen.
 *  The basic structure of the c++ program, and the main function.
 *
 */

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! " << endl;
    cout << "I like programming, programming is fun. " << endl;
    return 0;
}
The first section of the code is the comment, there are a few ways to include comments in our source code. The first way is only for a single line, it seems to be the easiest way. It is useful for variable descriptions or function's comment. The second one is good for comments that is a few lines. But for the second way to display comment, please remember to end the comment with */. Or else it will give you a compiler error. Of course, you can go fancy with that method. But some of the professors might not like it. They might prefer the first way, just add // to every single line.
// This is a comment

/* This comment can
   be written over
   a few lines, just remember
   to close it! */

/* 
 * Look at this
 * beautiful
 * comment style.
 *
 */

// One line of comment
// Another line of comment
// You can type as many single lined comment as you want
The first thing you see after the comment section will be the include statement, we will discuss it more in the later tutorial. For now just remember to type it when you want to do any kind of displaying messages, the include is to let the linker know I am including part of the codes from different header files. 
#include <iostream>
using namespace std;
The main function's name... is called main. And int represents integer, therefore main function will return an integer. The integer is used to determine the state of the function, whether it is not working, it crash, or anything important the program is going to telling the operating system or user. Usually 0 means it is normal, and the rest other is considered to be not "normal". But what it returns is purely based on the what the programmer wants it to be. The meat of this program is the following line.
cout << "Hello World! " << endl;
cout means the stream that the data is sending to, << is called the insertion operator, it will send the data into the stream. And endl; is a symbol to let the program knows it is going to go to the next line. It is possible to link the input data like the following to create something like this. Notice the last example, show you how to control how many line to skip.
cout << "Regular style, outputting message. " << endl;
cout << "There " << "are " << "more " << "messages. " << endl;
cout << "Let's skip 5 lines? " << endl << endl << endl << endl << endl;
Thoughts: This first tutorial is a lot longer than what I thought it will be... I did not expect it to be so lengthy. most programmers can write this piece of code within 20 seconds, when trying to explain it without any prior knowledge is a different story. And I will have to double check the facts of my explanation.

Practice Problem 1:
Write a program that print Good morning, Good afternoon, Good night in 3 separate lines. Tips, to go to next line, use endl. The output of the program should look like this.
Good morning
Good afternoon
Good night
See here for solution

No comments:

Post a Comment