本帖最後由 鄭繼威 於 2023-5-5 20:30 編輯
新增 "請輸入商品代碼:" 與 "數量:" 的選項,
並於結帳時計算出總共多少錢,執行畫面如下圖所示。
 - #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
-
- //商品名稱
- string name[]={"遙控汽車","飛機模型","足球\t","拼圖\t","玩具槍","可愛玩偶","籃球\t"};
- //商品價格
- int price[]={450,550,325,200,660,150,380};
-
- cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
- cout<<"[商品價目表]"<<endl;
-
- //把選項|名稱|價格輸出
- for(int i=0;i<=6;i++)
- {
- cout<<"("<<i+1<<")"<<name[i]<<"\t"<<price[i]<<endl;
- }
- cout<<"(8)結帳"<<endl;
-
- int option,qty,sum=0;
-
- re:
- cout<<"你要買什麼?";
- cin>>option;
- //1~7
- if(option>=1 and option<=7)
- {
- //購物
- cout<<"你要買幾個?";
- cin>>qty;
- sum=sum+(price[option-1]*qty);
- goto re;
- }
- else if(option==8)
- {
- //結帳
- goto checkout;
- }
- else
- {
- cout<<"輸入錯誤"<<endl;
- goto re;
- }
- checkout:
- cout<<"總共="<<sum<<endl;
- // cout<<"總共="<<price[option-1]*qty<<"錢"<<endl;
-
-
- system("pause");
- return 0;
- }
複製代碼 |