throw domain_error问题

都说没想好 2014-04-14 09:32:21
题目是输入两次考试成绩和几次家庭作业成绩,按比例算出总成绩,如果家庭作业成绩没做的情况抛出异常


#include<iostream>
#include<string>
#include<stdexcept>
#include<ios>
#include<iomanip>

using std::cin;
using std::cout;
using std::endl;

int main()
{
cout<<"please enter your first name:";
std::string name;
cin>>name;
cout<<"Hello, "<<name<<"!"<<endl;

cout<<"Please enter your midterm and final exam grades:";
double midterm,final;
cin>>midterm>>final;

ut<<"Enter all your homework grades,"
"followed by end-of-file:";

int count=0;
double sum=0;

double x;
while(cin>>x)
{
++count;
sum+=x;
}

if(count==0)
throw std::domain_error ("The Student has done no homework;");
//上面这句有什么错,我这样写,运行的时候如果不输入作业成绩,直接ctrl+z会给我一个终止框,我要的是能抛出异常并且给语句提示,怎么改~
std::streamsize prec=cout.precision();

cout<<"Your final grade is"<<std::setprecision(3)
<<0.2*midterm+0.4*final+0.4*sum/count
<<std::setprecision(prec)<<endl;

return 0;
}
...全文
229 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
都说没想好 2014-04-14
  • 打赏
  • 举报
回复
引用 1 楼 buyong 的回复:
try{...
if(***)
   throw something;
....
}
catch(...)
{
}
可以具体给我代码吗,我试了试 try{ if(count==0) throw std::domain_error ("The Student has done no homework;"); }catch (std::domain_error){ } std::streamsize prec=cout.precision(); cout<<"Your final grade is"<<std::setprecision(3) <<0.2*midterm+0.4*final+0.4*sum/count <<std::setprecision(prec)<<endl; 结果他直接给我算了乱码答案出来了。。
buyong 2014-04-14
  • 打赏
  • 举报
回复
try{...
if(***)
   throw something;
....
}
catch(...)
{
}

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧