Accelerated C++ Solution to Exercise 1-0

Exercise 1-0

Compile, execute, and test the programs in this chapter.

Solution

I will be taking the programs directly from the book (chapter 1 – Working with Strings), test run them, and see what we get. This exercise is purely to get our hands dirty right away on writing a submitting very simple programs on handling strings. Nothing too clever.

Test (Program 1)

The goal of this program is to

  • ask for a person’s name,
  • read the name in as a string variable, and
  • output a greeting message.
// ask for a person's name, and greet the person
#include <iostream>
#include <string>

int main()
{
    // ask for the person's name
    std::cout << "Please enter your first name: ";

    // read the name
    std::string name;   //define name
    std::cin >> name;   //read into name

    // write a greeting
    std::cout << "Hello, " << name << "!" << std::endl;
    return 0;
}

Result (Program 1)

Run the program. The followings are produced in the console output window:

Please enter your first name:

Type in some texts (my name) on the keyboard

Please enter your first name: Johnny

Hit enter key and it gives:

Please enter your first name: Johnny
Hello, Johnny!

Process returned 0 (0x0)   execution time : 2.631 s
Press any key to continue.

Test (Program 2)

The goal of program 2 is to read in a person’s name, and generate a framed greeting like this:

******************
*                *
* Hello, Johnny! *
*                *
******************

To do this the following C++ program is taken directly from the book.

// ask for a person's name, and generate a framed greeting
#include <iostream>
#include <string>

int main()
{
    std::cout << "Please enter your first name: ";  //ask for the person's name
    std::string name;   //define name
    std::cin >> name;   //read into name

    // build the message that we intend to write
    const std::string greeting = "Hello, " + name + "!";

    // build the second and forth lines of the output
    const std::string spaces(greeting.size(),' ');
    const std::string second = "* " + spaces + " *";
    const std::string first(second.size(),'*');

    // write it all
    std::cout << std::endl;
    std::cout << first << std::endl;
    std::cout << second << std::endl;
    std::cout << "* " << greeting << " *" << std::endl;
    std::cout << second << std::endl;
    std::cout << first << std::endl;
    return 0;
}

Result (Program 2)

Run the program. The followings are produced in the console output window:

Please enter your first name:

Type in some texts (my name) on the keyboard

Please enter your first name: Johnny

Hit enter key and it gives:

Please enter your first name: Johnny

******************
*                *
* Hello, Johnny! *
*                *
******************

Process returned 0 (0x0)   execution time : 2.520 s
Press any key to continue.

Some core learning:

  • Use double quote to define string literal. Use single quote to define character (char) literal.
  • We can use + to concatenate a string and a string literal (and vice versa), or a string and a string, but not a string with a string literal (nor vice versa). i.e.
    • string + string is okay.
    • string literal + string is okay.
    • string + string literal is okay.
    • string literal + string literal is NOT okay.
  • Use the object component size() , which is a function, to determine the length (number of characters) of a string (the object itself). e.g. greeting.size gives the length of the string greeting.
  • Refer to the book to see the various ways to define a string variables, including the use of overload.
  • Keep It Simple and Stupid (KISS) – write the code in a way that is easy to write and understand. Nothing too clever.

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000