1. 程式人生 > >c++ primer plus 複合型別練習題

c++ primer plus 複合型別練習題

1.本題要求:

編寫一個C++程式,如下述輸出示例所示那樣請求並顯示資訊

what is your first name? Betty Sue

what is your last name? Yewe

what letter grade do you deserve? B

what is your age? 22

Name:Yewe

grade: C

age:22

解題思路:

符合型別這一章裡面介紹了結構體,因此採用結構體最合適

本題稍微麻煩的地方在於 輸入B變成C,試了半天用強制轉換搞定了,程式碼貼上

#include <iostream> struct human  {     char name[30];     char Lname[20];     char grade;     int age; }; int main() {     using namespace std;     human *p = new human;     cout << "what is your first name?\n";     cin>> (*p).name;     cout << "what is your last name?\n";     cin >> (*p).Lname;     cout << "what letter grade do you deserve?\n";     cin >> (*p).grade;     cout <<"what is your age?\n" ;     cin >> (*p).age;     cout << "Name:" <<(*p).name<< "," << (*p).Lname<<"\n";     cout << "grade:" << (char)((*p).grade +1)<< "\n"; //把B變成C,強制轉換     cout << "age:"<<(*p).age;     delete p;     return 0; }

執行結果