尝试使用负整数函数进行捕获
Try catch using a function for negative integers
所以基本上我试图让它停止重复。如果我正确输入数字,它就可以正常工作。如果我输入不允许的负数并且需要 try-catch 异常,它会不断重复并且不会停止询问数字。
我所拥有的只是代码的源文件,我正在尝试为 main 创建一个函数。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void gcd(int x, int y);
int main()
{
int x;
int y;
cout << "Please enter two integer values" << endl;
cin >> x;
cin >> y;
gcd(x, y);
return 0;
}
void gcd(int x, int y)
{
int gcd;
int s = 0;
while (s == 0)
{
try
{
if (x < 0 || y < 0)
throw 1;
else
{
s == 1;
break;
}
}
catch (int x)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
cin >> x >> y;
continue;
}
}
for (int i = 1; i <= x && i <= y; i++)
{
if (x % i == 0 && y % i == 0)
gcd = i;
}
cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd << endl;
}
如果您不想用负值调用函数 gcd()
,请抛出 std::invalid_argument
异常。请求用户输入不是 gcd()
的职责。在调用 gcd()
.
之前验证 main()
中的输入
#include <limits>
#include <stdexcept>
#include <iostream>
int gcd(int, int);
int main()
{
int x, y;
while (std::cout << "Please enter two positive integers: ",
!(std::cin >> x >> y) || x < 0 || y < 0)
{
std::cerr << "Input error :(\n\n";
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
std::cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd(x, y) << "\n\n";
}
int gcd(int x, int y)
{
if (x < 0 || y < 0)
throw std::invalid_argument("No negative arguments to gcd(), please :(");
return y == 0 ? x : gcd(y, x % y);
}
您可以(也许应该)从 gcd
函数中删除逻辑,并将其放置在您从用户那里获得输入的地方,即 main
中。另外,提前说明要求。例如:
int main()
{
int x;
int y;
cout << "Please enter two positive integer values" << endl;
cin >> x;
cin >> y;
if (x < 0 || y < 0)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
return 0;
}
gcd(x, y);
return 0;
}
现在,您可以在 gcd
中放置断言以强制没有负值进入:
void gcd(int x, int y)
{
assert(x >= 0);
assert(y >= 0);
// ...
}
所以基本上我试图让它停止重复。如果我正确输入数字,它就可以正常工作。如果我输入不允许的负数并且需要 try-catch 异常,它会不断重复并且不会停止询问数字。 我所拥有的只是代码的源文件,我正在尝试为 main 创建一个函数。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void gcd(int x, int y);
int main()
{
int x;
int y;
cout << "Please enter two integer values" << endl;
cin >> x;
cin >> y;
gcd(x, y);
return 0;
}
void gcd(int x, int y)
{
int gcd;
int s = 0;
while (s == 0)
{
try
{
if (x < 0 || y < 0)
throw 1;
else
{
s == 1;
break;
}
}
catch (int x)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
cin >> x >> y;
continue;
}
}
for (int i = 1; i <= x && i <= y; i++)
{
if (x % i == 0 && y % i == 0)
gcd = i;
}
cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd << endl;
}
如果您不想用负值调用函数 gcd()
,请抛出 std::invalid_argument
异常。请求用户输入不是 gcd()
的职责。在调用 gcd()
.
main()
中的输入
#include <limits>
#include <stdexcept>
#include <iostream>
int gcd(int, int);
int main()
{
int x, y;
while (std::cout << "Please enter two positive integers: ",
!(std::cin >> x >> y) || x < 0 || y < 0)
{
std::cerr << "Input error :(\n\n";
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
std::cout << "The gcd of x: " << x << " and y: " << y << " is: " << gcd(x, y) << "\n\n";
}
int gcd(int x, int y)
{
if (x < 0 || y < 0)
throw std::invalid_argument("No negative arguments to gcd(), please :(");
return y == 0 ? x : gcd(y, x % y);
}
您可以(也许应该)从 gcd
函数中删除逻辑,并将其放置在您从用户那里获得输入的地方,即 main
中。另外,提前说明要求。例如:
int main()
{
int x;
int y;
cout << "Please enter two positive integer values" << endl;
cin >> x;
cin >> y;
if (x < 0 || y < 0)
{
cout << "Wrong negative input please type in two Positive integers" << endl;
return 0;
}
gcd(x, y);
return 0;
}
现在,您可以在 gcd
中放置断言以强制没有负值进入:
void gcd(int x, int y)
{
assert(x >= 0);
assert(y >= 0);
// ...
}