Basic C++ Programming: Test Your Knowledge

Test your knowledge of the absolute basics of C++.

The exercises below are designed for beginners who've been learning C++ for just a few weeks. Or months; I'm assuming you've got on to creating header files, and perhaps you haven't if you've only been learning a few weeks; C++ is hard!

If you get stuck anywhere, don't be disheartened -- Rome wasn't built in a day! Check the answer, cover it up again and see if you can write the code from memory.



You can click the link below each question to reveal the answer.

These exercises were created using the free CodeBlocks IDE for Windows, using the excellent GNU compiler.

1. Create a "Hello World" C++ Program.



Create a basic C++ program that simply outputs the text “Hello World”.






An Interactive Program


Create a program that asks the user to enter an integer. If the integer is less than 10, print the message "This number is too small". If the integer is greater than or equal to 10, print "This number is big enough".

Hint: use cin with an if ... else statement.


Arrays


Create a program that creates an array of five hard-coded floating-point values, then prints out just the second value.


Arrays and Loops


Modify the above program so that it uses a for loop to display all the values in the array, all on the same line, each value formatted to two decimal places and followed by a space.

Hint: you can hardcode the number of elements in the array for your 'for' loop, OR for extra credit figure out how to use sizeof to get the number of elements in the array.

Use setprecision from the iomanip header.

If you resort to using printf(), you get half credit .... :)

But my advice is, don't be afraid to Google the right answer. Or just peek. It's a good way to learn.


Two Dimensional Arrays


A bit trickier, this one. Write an application that creates a two-dimensional array of doubles, with two rows and three columns. Print the value in the second row and third column.

Hint: 2D arrays can be declared using [][] instead of []. But you'll need to put the actual number of array elements into these square brackets. Hey, it's not Java!

When you initialize the array (put actual numbers into it), remember that each element is an array in itself.

If while attempting this question, you suffer a severe psychotic reaction and have to be placed in a special backwards jacket for your own protection, try looking at the answer, then cover it up again and see if you can remember how to do it. Once you've got your arms free, that is.


Looping Through 2D Arrays


Create an application that uses two nested for loops to loop through the 2D array defined above and print the values.

Hint: Here's the outer loop:

 for(int row = 0; row < 2; row++) {
} 

The inner loop should use a loop variable called "col" (or whatever you like) and should loop through the columns.

Hard code the number of columns and rows, as above.

Once again, this is one of the trickiest beginner's tasks in C++. If you can't work it out, check the answer, then hide it again and try to write it from memory.


Create Classes and Objects


First, create a main program as in the first exercise.

Next, define a new class in its own .cpp and .h file. Call the class Car. Give it a single method called "start". Make the method simply print "Car started!".

In your main program, create a new Car object and call its start() method.

Your final program should simply therefore display the text "Car started!".







Constructors


Modify the above Car class so that it has an instance variable called name of type string (or whatever kind of string or String is defined for your compiler). Add a constructor that accepts a string parameter and sets the car's name using this parameter. Add a getName() method that returns the car's name.

Finally, modify the main application so that it sets the car's name via the constructor, then prints the cars name (retrieving it using getName()).






While Loops


Write an application that asks the user to enter the number '5' and loops over and over until '5' is entered.

When 5 is finally entered, print "Got it!".

Hint: use cin.

The program will crash if you enter something that isn't a number, but don't worry about that.



Crash-Proof Input


The above program crashes if a user enters something other than a number. The problem is that we blindly assume that the user will enter an integer.

Modify the program so that no input can crash it.

Hint: use cin.getline() to get a line of text, then convert the text to an int (if possible) using, for example, istringstream.

Don't be afraid to Google this stuff.

This stuff's so tricky in C++ that I hesitate to include it here .... but I figure that with a bit of Googling, you'll be OK. After all, programming is as much about searching for answers as it is actually doing stuff. Or am I thinking of religion?

If this stuff is really too much for your head, feel free to skip this question! Or better still, check the answer then try to do it from memory!



Switch Statements


Write a program that asks the user to enter an integer. If the user enters '1', print "Got 1". If the user enters '66', print "Got 66". If the user enters something other than these two numbers, print "Got something else".

The program should use a switch statement.

Hint: you may need to look up switch statements on Google. Use the default clause to implement the case where the user doesn't enter '1' or '66'.



Do...While Loops


A while loop checks its condition before the first iteration of the loop. A do...while loop checks the condition at the end of the loop. This means there's always at least one iteration of the loop.

Write a program that asks the user to enter an integer, then gets the input from the user. The program should repeatedly ask the user to enter an integer until the user enters an integer greater than 10; then it should print "Integer greater than 10 detected!" and should end.

The program must contain only two cout statements!

Hint: use a do...while loop to enclose the 'prompt' (i.e. the text that asks the user to enter the integer) and the bit that gets the user input.



Success?


Once you can do this lot from memory, you've mastered the absolute basics of C++. Hats off to you; C++ is tricky to learn and unforgiving. If you can do this lot, you can learn the rest of it too.

If you want a SLIGHTLY easier ride, you can always try Java; take the basic Java test here.

If you're looking for 1-to-1 lessons in programming via Skype, don't hesitate to contact me: check out my in-yer-face advertising hype here

I've been doing C++ for well over a decade now, and believe me it seemed like a black art when I first encountered it in 1997. So if you've having problems with it --- don't give up! It gets easier, and you've already got through the hardest bit!