默认参数函数

用c++实现点餐

  1. 函数重载
  2. 结构使用
    3.带默认参数函数 函数生明时,写入初值,函数定义时不要写入,否则编译错误
*          #include<iostream>      order a meal!
#include<string>
#include<iomanip>
using namespace std;
typedef struct dish
{
  char name[20];
  float price;
}meal;
void PayTheBill(float total,float discount=1);

int main(void)
{
    meal table[5]={{"fish",50.5},{"bread",10.5},{"milk",5.0},{"cake",40.5},{"chocolate",15}};
   for(int i=0;i<5;i++)
{
    cout<<table[i].name<<"  "<<table[i].price<<endl;
}
int flag,ending,amount;
float total=0.0;
  cout<<"please choose what you want from the top"<<endl;
 for(int i=0;i<5;i++)
 {
    cout<<"do you want "<<table[i].name<<"? yes/1  no/0 and amount?"<<endl;
    cin>>flag;

    if(flag)
    cout<<"how many?"<<"  ";
    cin>>amount;
    total=total+ (table[i].price)*amount;
    if(i<=4)
    {
      cout<<"do you want more? yes/1 no/0"<<endl;
    cin>>ending;
    if(!ending)
    break;
    }
    else
    cout<<"that's all,have a good time"<<endl;
 }

 cout<<"if a vip? yes/1 no/0"<<endl;
 cin>>flag;
 if(flag)
 {
   PayTheBill(total,0.8);
 }
 else
 {
   PayTheBill(total);
 }
 return 0;
}
 void PayTheBill(float total,float discount)   //even though don't have a return ,void cant be delete function define  not allowed to give defalut
 {
     float n;
     n=total*discount;
     cout<<"sum up to "<<n<<endl;
     cout<<"have a good time!";
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值