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?

Debugging

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.

step_into1

step_mac1

step_mac2

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,

0,1,1,2,3,5,8,13,21,34

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

print

8 Comments

  1. Alvin & Johnatan said:

    Challenge 2

    #include
    #include
    int main (int argc, const char * argv[])
    {
    int sum1 = 0;
    int sum2 = 1;
    int totalSum = 0;

    for (int i = 0 ; i < 9 ; i++) {
    std::cout << totalSum << std::endl;
    totalSum = sum1 + sum2;
    sum1 = sum2;
    sum2 = totalSum;

    return 0;
    }

    August 25, 2015
    • Stanley Sim said:

      Almost. But the start shows 0,1,2,3,… which is not correct. It should be 0,1,1,2,3,…
      How to rectify this bug?

      August 25, 2015
      • Alvin & Johnatan said:

        #include
        #include
        int main (int argc, const char * argv[])
        {
        int zero = 0;
        int sum1 = 0;
        int sum2 = 1;
        int sum3 = 0;
        int totalSum = 0;

        for (int i = 0 ; i < 2 ; i++)
        {
        std::cout << totalSum << std::endl;

        totalSum = sum1 + sum2;
        sum1 = sum2;
        sum1 = sum3;
        sum3 = totalSum;
        }

        for (int i = 0 ; i < 8 ; i++)
        {
        std::cout << totalSum << std::endl;

        totalSum = sum1 + sum2;
        sum1 = sum2;
        sum2 = totalSum;
        }
        return 0;
        }

        August 25, 2015
        • Stanley Sim said:

          Good try, but give yourself some more time to write a more elegant code. At least this works.

          August 25, 2015
          • Alvin & Johnatan said:

            int main(int argc, const char * argv[])

            {
            int A = 0;
            int B = 1;
            int C = 0;

            for (int i = 0 ; i < 10 ; i++)
            {
            if ( C = '1' )
            {
            std::cout << A << std::endl;
            }

            else
            {
            std::cout << C << std::endl;
            }

            C = A + B;
            A = B;
            B = C;
            }

            return 0;
            }

            August 31, 2015
          • Stanley Sim said:

            Program doesn’t run. Syntax error. Did you try it?

            Try this:

            int x = 0, y = 1, z = 0;

            std::cout << x << std::endl; std::cout << y << std::endl; for (int i = 0; i < 8; i++) { z = x + y; std::cout << z << std::endl; x = y; y = z; }

            August 31, 2015
    • Alvin & Johnatan said:

      Correction:

      }
      return 0;
      }

      August 25, 2015
  2. Alvin said:

    Tried it, sir. It worked and yours too.

    August 31, 2015

Comments are closed.