『C++によるプログラミングの原則と実践』 第3章 オブジェクト、型、値のドリル問題
田中です。引き続き、ドリル問題をやっていきます。
手紙の簡単なテンプレートを生成するプログラムを作成します。ソースは以下の通りです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #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'; } |