返回列表 發帖

購物系統 (二)

本帖最後由 tonyh 於 2016-8-6 11:11 編輯

新增 "請輸入商品代碼:" 與 "數量:" 的選項,
並於結帳時計算出總共多少錢, 執行畫面如下圖所示.

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int p,q,sum=0;
  7.     string name[7]={"遙控器車",
  8.                     "飛機模型",
  9.                     "足球    ",
  10.                     "拼圖    ",
  11.                     "玩具槍  ",
  12.                     "可愛玩偶",
  13.                     "籃球    "};
  14.     int price[7]={450,550,325,200,660,150,380};
  15.     cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
  16.     cout<<"[商品價目表]"<<endl;
  17.     for(int i=0; i<7; i++)
  18.     {
  19.         cout<<"("<<i+1<<")"<<name[i]<<"\t"<<price[i]<<"元"<<endl;
  20.     }
  21.     cout<<"(8)結帳"<<endl<<endl;
  22.     while(true)   //無窮迴圈
  23.     {
  24.         cout<<"請輸入商品代碼: ";
  25.         cin>>p;
  26.         if(p==8)
  27.         {
  28.             break;      //跳出所在迴圈
  29.         }else if(p>=1 && p<=7)
  30.         {
  31.             cout<<"數量: ";
  32.             cin>>q;
  33.             if(q<=0)
  34.             {
  35.                 cout<<"輸入錯誤!"<<endl;
  36.                 continue;   //跳下一輪迴圈     
  37.             }     
  38.             sum+=(price[p-1]*q);
  39.         }else
  40.         {
  41.             cout<<"輸入錯誤!"<<endl;
  42.             continue;   //跳下一輪迴圈
  43.         }
  44.     }
  45.     cout<<endl<<"總共"<<sum<<"元!"<<endl;
  46.     system("pause");
  47.     return 0;
  48. }
複製代碼

返回列表