- 分享
- 0
- 人气
- 10
- 主题
- 1234
- 帖子
- 5729
- UID
- 187410
- 积分
- 13908
- 阅读权限
- 25
- 注册时间
- 2008-12-21
- 最后登录
- 2018-4-5
- 在线时间
- 5305 小时
无敌名嘴
成功之前我们要做应该做的事情,成功之后我们才可以做喜欢做的事
|
zyang94 发表于 2013-7-30 11:58 PM
#include
#include
题目;http://www.scribd.com/doc/154330181/2013-c
question 1(a)
我要做的是:
,user key-in 后,我可以 check 看他 key in 的 是否是 号码(integer),如果不是,要求他再 key in 过
purchase price不可能可以key in英文字母,然后又可以run得到吧?》我就是要弄掉它。
例子:
cout<< "The purchase price (RM)\t\t: ";
cin>> purchase_price;
如果 user 随便 key in "A(不是号码),就 show error message 给他,并要求他 key in 正确号码
我只做得到 if (!(cin >> purchase_p)) cout << "non numerical data!" << endl<<endl;
else
用cin.fail的话,可以吗?
但是出现后面的东西,要怎样弄掉?
STRING的问题的解决办法,我还想不到!就是USER KRY IN"fggdf"的话。
我的code在这里::- #include<iostream>
- #include<iomanip>
- #include<string>
- using namespace std;
- int main()
- { // Declare variables
- int year=0;
- double purchase_p ,depreciation_r,end=0,min_value,depreciation_amount = 0,end_of_year_value,accumulated_d = 0;
- cout<<"This program computes and displays the values of your vehicle"
- <<" from the year\t"<<"purchased until the value drops below a minimum value."<<endl<<endl;
- cout<<"Enter the purchase price: RM ";
- cin>>purchase_p;
- if (!(cin >> purchase_p))
- cout << "non numerical data!" << endl<<endl;
- else
- cout<<"Enter the depreciation rate(%): ";
- cin>>depreciation_r;
- if (!(cin >> depreciation_r))
- cout << "non numerical data!" << endl<<endl;
- else
- cout<<"Enter the minimum value: RM ";
- cin>>min_value;
- cout<<endl<<endl;
- if (!(cin >> min_value))
- cout << "non numerical data!" << endl<<endl;
- else
- return 0;
- cout<<"YEAR\t"<<"DEPRECIATION\t"<<"END OF YEAR\t"<<"ACCUMULATED"<<endl
- <<"\t"<<" AMOUNT\t"<<" VALUE\t"<<"DEPRECIATION"<<endl
- <<"\t <RM>"<<"\t\t <RM>"<<"\t\t <RM>"<<endl;
- cout<<string(80,'-')<<endl;
- do
- {
- if (year == 0)
- depreciation_amount = 0;
- else
- depreciation_amount=(purchase_p * depreciation_r/100);
- end_of_year_value = purchase_p - depreciation_amount;
- purchase_p-= depreciation_amount;
- accumulated_d += depreciation_amount;
- cout<<year<<"\t"<<setw(8)<<fixed<<setprecision(2)<<depreciation_amount<<"\t"<<setw(9)<<setprecision(2)<<end_of_year_value<<"\t"<<setw(9)<<setprecision(2)<<accumulated_d<<endl;
- year++;
- } while ( purchase_p > min_value );
- year -= 1;
- cout<<"\n"<<"It will take "<<year<<"years for your vehicle to drop below RM 2500.00"<<endl;
- return 0;
- }
复制代码 |
|