Lesson 4: Strings, if and for statements

In last lesson, we covers std::string and basic logic using if. Let’s study the example below.

#include <iostream>
int main() {
    char gender = ''; // single quotes are for char, double quotes are for string.
    std::cin >> gender;
    if(gender = 'm') std::cout << "Hi Sir!";
    else std::cout << "Hi Ms!";

    return 0;

Before typing this into you IDE, suggest what it does. Now, type this code and run. Does it perform what you expect?

More std::string

Challenge 1

Refer to the C++ documentation for std::string. Now, you will write a program that test whether a string entered in cin is more than 10 characters or not. First, look for a method that can tell you the length of a string. If it is, then print “This word is too long.”. Otherwise, print “This word is within acceptable length.”.

Hint: You will need to read in a string and use logical if to decide whether the string is within acceptable length or not.

For loop

If you want to perform a fix number of repetitions, use a for loop. Study the code below and suggest what it does.

#include <iostream>
int main(int argc, const char * argv[]) {
    int sum = 0;

    for (int i = 0 ; i < 10 ; i++) {
        sum = sum + i;
    std::cout << sum;
    return 0;

Is the output the same as what you expect?


If you were expecting 55 but get a 45 from the console, you will need to debug what is wrong with the program. One way to do this is to trace the runtime step by step. To do this, we need to use the step functions of your IDE. For Xcode, you need to insert a breakpoint by clicking on the leftmost bar. Then use the step function below to run step by step.




You will be able to see the values of the variables changing as the program runs. Use this to find out why the sum is 45 and not 55. Then modify the program to make it sum to 55.

Challenge 2

Write a program to print the first 10 Fibonacci numbers. Fibonacci numbers are numbers that are sum of the previous 2 integers. For example,


If your code runs successfully, share it by copy and paste to the comment in this post.

Introduction to C++ Programming

In this course, we are going to use C++ for competitive programming. The goal for this course is to participate in the University of Waterloo’s Canadian Computing Competition in Feb 2016.

There are two levels of competition. If since all of you have little programming experience, we can only aim for the Junior Level competition. However, do not under-rate this Junior Level competition as some of the questions can be tricky too.

What software should I install?

C++ is a cross platform programming language. There are various tools available to do C++ programming on both Windows and the Mac platform. For Windows users, Visual Studio 2015 Community Edition is a very powerful Integrated Development Platform(IDE). This is the preferred development environment for our course for Windows users. Please check the C++ option when installing, as the default installation only installs C#. However, this software is large, and it is not possible for you to download and install in School (we are in Indonesia). If you are not able to install VS2015 in time for the training, then please install this light weight C++ IDE Dev-C++. It’s only 50MB.

For Mac users, the ideal IDE for C++ development is Xcode. This IDE is also huge, and you need to install prior to attending the training.

If you really can’t install anything in time for the training, then you can still use online compilers such as IDEOne. However, this is only a stop gap solution. Please install proper software once you have the chance.