Exercises
Exercise 2
Swathe — Sat, 2011-03-26 09:33
In our second exercise we moved on to inputting text and printout out the results. The code below asks you for your name and then says hello :)
Open your preferred text editor, input the text in bold below and then save it as name.cpp
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
cout << "Enter your name: " ;
cin >> name;
cout << "Hello "<< name << endl;
return 0;
}
Now in the terminal type:
$ g++ name.cpp -o name
You should now be able to run your newly written program with the command:
$ ./name
Enjoy!
Exercise 1
belly — Sat, 2011-03-05 08:45
This exercise has been designed to create and then compile your first C++ program as part of the basic “teachings” of using the GNU “g++” compiler.
In the tradition with initial programming, this exercise will focus on creating a “Hello World” C++ program.
A "Hello world" program is a computer program that prints out "Hello world" on a display device. It is typically one of the simplest programs possible in most programming languages. Therefore, by tradition, it often the first program taught in a beginning class on a particular language. It is also used to illustrate the most basic syntax of a programming language. Cited (http://en.wikipedia.org/wiki/Hello_world_program)
Therefore I would ask those interested to try out the following:Create a hello.cpp file using your favourite text editor containing the following:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
- Compile your newly created C++ program using the following command line syntax (note, if you don’t have g++ installed, you will need to install it)
g++ hello.cpp -o hello
- Then execute your newly compiled program using the following command:
./hello
- You should see the following output
$ g++ hello.cpp -o hello
$ ./hello
Hello World!
