返回列表 發帖

購物系統 (二)

本帖最後由 鄭繼威 於 2023-5-5 20:30 編輯

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

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.         
  7.         //商品名稱
  8.     string name[]={"遙控汽車","飛機模型","足球\t","拼圖\t","玩具槍","可愛玩偶","籃球\t"};
  9.     //商品價格
  10.         int price[]={450,550,325,200,660,150,380};
  11.        
  12.         cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
  13.     cout<<"[商品價目表]"<<endl;
  14.    
  15.     //把選項|名稱|價格輸出
  16.     for(int i=0;i<=6;i++)
  17.     {
  18.             cout<<"("<<i+1<<")"<<name[i]<<"\t"<<price[i]<<endl;
  19.         }
  20.         cout<<"(8)結帳"<<endl;
  21.    
  22.     int option,qty,sum=0;
  23.    
  24.     re:
  25.     cout<<"你要買什麼?";
  26.         cin>>option;
  27.         //1~7
  28.         if(option>=1 and option<=7)
  29.         {
  30.                 //購物
  31.                 cout<<"你要買幾個?";
  32.                 cin>>qty;
  33.             sum=sum+(price[option-1]*qty);
  34.             goto re;
  35.         }
  36.         else if(option==8)
  37.         {
  38.                 //結帳
  39.                 goto checkout;
  40.         }
  41.         else
  42.         {
  43.                 cout<<"輸入錯誤"<<endl;
  44.                 goto re;
  45.         }

  46.         checkout:
  47.                 cout<<"總共="<<sum<<endl;
  48. //        cout<<"總共="<<price[option-1]*qty<<"錢"<<endl;

  49.        
  50.    
  51.     system("pause");
  52.     return 0;
  53. }
複製代碼

5

TOP

回復 4# 張絜晰

TOP

返回列表