大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

『C++によるプログラミングの原則と実践』 第3章 オブジェクト、型、値のドリル問題

株式会社クローバーフィールドの経営理念
著者:田中陽昌
公開日:2020/07/30
最終更新日:2020/07/30
カテゴリー:雑記

田中です。引き続き、ドリル問題をやっていきます。
手紙の簡単なテンプレートを生成するプログラムを作成します。ソースは以下の通りです。

#include "std_lib_facilities.h"

int main()
{
        cout << "Enter the name of the person you want to write to\n";
        string first_name;
        cin >> first_name;
        cout << "Enter the age of the recipient\n";
        int age;
        cin >> age;
        if(age <= 0 || age >= 110)
        {
                simple_error("you're kidding!");
        }
        cout << "Enter the name of the person you want to talk about\n";
        string friend_name;
        cin >> friend_name;
        cout << "Enter the gender of that person \n";
        char friend_gender = '0';
        cin >> friend_gender;
        cout << "Enter Your Name\n";
        string name;
        cin >> name;
        // ここから手紙の出力
        cout << "Dear " << first_name << "\,\n";
        cout << " How are you? I am fine\. I wish this year was over\.\n";
        cout << "Have you seen " << friend_name << " lately?\n";
        if (friend_gender == 'm')
                cout << "If you see " << friend_name << " please ask him to call me.\n";
        else if(friend_gender == 'f')
                cout << "If you see " << friend_name << " please ask her to call me.\n";
        cout << "I hear you just had a birthday and you are " << age << " years old.\n";
        if (age < 12)
                cout << "Next year you will be " << age + 1 << "\.\n";
        else if (age == 17)
                cout << "Next year you will be able to vote\.\n";
        else if (age >= 70)
                cout << "I hope you are enjoying retirement\.\n";
        cout << "Yours sincerely\n\n\n";
        cout << name << '\n';
}

実行結果はこのようになります。

    上に戻る