cout-setfill-setw的函式庫:iomanip
try-threw-catch的函式庫:exception、stdexcept
Input:
輸入合理成績n(0<=n<=100)
運用cout-setfill-setw,使得2位數、3位數前面空格補0。例如001、060。
若不合理輸出invalid brake
output:
輸出成績
若不合理輸出
---
#include <iostream>
#include <iomanip>
#include <exception>
/*#include <stdexcept>*/
using namespace std;
class my_score
{
public:
my_score()
{
score = 0;
}
void Input_score(int n)
{
if(n<0||n>100)
{
throw invalid_argument{
"invalid brake"
};
}
score = n;
}
void print_score()
{
cout<<setfill('0')<<setw(3)<<score<<endl;
}
private:
int score;
};
int main()
{
int n;
my_score obj01;
for(;cin>>n;)
{
try
{
obj01.Input_score(n);
obj01.print_score();
}
catch(invalid_argument &e)
{
cout<<"Exception: "<<e.what()<<endl;
}
}
return 0;
}
留言列表