关于程序变量就近原则的实例

//#include <stdafx.h>
#include <iostream>
using namespace std;
class Menu
{
private:
int mune_number;
int object;
int sum_price;
int customer;

public:
void foodname();
void price();


};
void Menu::foodname() 
{
cout<<"Happy Everday!"<<endl;
cout<<"                             "<<endl;
cout<<"Calclation Systeem: "<<endl;
cout<<"In this system, you can calculate the bill depend on your input! Thanks!"<<endl;
cout<<"Please input the number of the customer"<<endl;
 int sum_price=0;//由于在类的私有权限中,已经定义了这两个变量,所以在该程序运行的过程中,程序会执行就近原则,而去重新调用没有赋值的变量,导致最终该变量

int customer=0;//的值悬空出现垃圾值

cin>>customer;
cout<<"OK!"<<customer<<" "<<"person in the bill."<<endl;
sum_price +=3*customer;
cout<<"The menu shows: "<<endl;
cout<<"Food: "<<endl;
cout<<"Code            Name                           Price "<<endl;
cout<<"1               Curry beef with rice           ¥40.00"<<endl;
cout<<"2               Sushi set meal                 ¥65.00"<<endl;
cout<<"3               YangZhou fried rice            ¥45.00"<<endl;
cout<<"4               Sirloin Steak with Spaghetti   ¥72.00"<<endl;
cout<<"5               Chicken vegetable roll         ¥42.00 "<<endl;
cout<<"Drink:  "<<endl;
cout<<"21              Soft drink                     ¥10.00 "<<endl;
cout<<"22              Red wine                       ¥15.00"<<endl;
cout<<"23              Beer                           ¥15.00"<<endl;


}
void Menu::price() 
{
int object=1;
while (object==1)
{
cout<<"Please input the code of the order:( one time one code)" <<endl;
cout<<"Digital only input is 1,2,3,4,5,21,22,23."<<endl;
cin>>mune_number;
switch ( mune_number )
{
case 1:
sum_price +=40;
break;
case 2:
sum_price +=65;
break;
case 3:
sum_price +=45;
break;
case 4:
sum_price +=74;
break;
case 5:
sum_price +=42;
break;
case 21:
sum_price +=10;
break;
case 22:
sum_price +=15;
break;
case 23:
sum_price +=15;
break;
}
cout<<"Any more ?(1-Yes,2-No)"<<endl;
cin>>object;
if (object==2)
break;
}
cout <<"The total price is "<<sum_price<<endl;
}
int main ()
{
Menu object;
object.foodname();
object.price();
system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值