65,144
社区成员
发帖
与我相关
我的任务
分享
//
#include <iostream>
#include <stdexcept>
using std::cin;
using std::cout;
using std::endl;
void keep_window_open();
int main()
{
int dividend=1, divisor=1;
cout<<"\nInput the dividend:\t";
cin>>dividend;
cout<<"\nInput the divisor:\t";
cin>>divisor;
if(divisor <=0)
throw runtime_error("\nNo! The divisor must be a positive number!\n");
keep_window_open();
}
void keep_window_open()
{
cout << "\nPress any key to exit:";
getchar();
}
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::runtime_error;
void keep_window_open();
int main()
{
int dividend=1, divisor=1;
int result=1;
char decide1 = 'y', decide2 = 'y';
while(decide1 == 'y')
{
cout<<"\nInput the dividend:\t";
cin>>dividend;
try
{
if(dividend<0)
{
throw runtime_error("For convenience, please make sure dividend is non-negative!\n");
}
decide1 = 'n';
while(decide2 == 'y')
{
cout<<"\nInput the divisor:\t";
cin>>divisor;
try
{
if(divisor<=0)
throw runtime_error("For convenience, divisor must be positive!\n");
decide2 = 'n';
result = dividend/divisor;
}catch(runtime_error err)
{
cout << err.what()
<< "\n Try Once More? Enter y or n "<<endl;
cin >>decide2;
}
}
}catch(runtime_error err)
{
cout << err.what()
<< "\n Try Again? Enter y or n" << endl;
cin >>decide1;
}
}
cout <<"\nSo the quotient of "<<dividend<<" divided by "<<divisor<<" is:\t"<<result<<endl;
keep_window_open();
}
void keep_window_open()
{
cout << "\nPress any key to exit:";
getchar();
}
#include<cstdio>
另外,你可以用
std::cin.get();
取代
getchar();