我已经构建了一个代码,通过获取字符串长度并找到输入的每个数字的总和并确定其是否能被9整除,来检查输入的数字是否可以正确地除以9。
代码运行良好,我只需要进行一些错误检查,就可以尝试添加两件事。
我首先需要确保输入的信息只是数字,没有字母。
我还想找到一种将输入限制为1到1000的方法。
我有一个布尔示例,该示例将检查实际上是否为数字,但是如何为我的userNum输入创建if语句以检查布尔值?
bool isNumber(const string &line)
{
if (line[0] == '0') return true;
return (atoi(line.c_str()));
}
#include <iostream>
#include <string>
using namespace std;
string userNum;
char userInput;
int sum, i, n;
int main()
{
tryagain:
sum = 0;
cout << "Enter number to test if it is divisible by 9: ";
cin >> userNum;
if (userNum == isNumber) {
cout << "Nah..";
return 0;
// Find sum of digits by seperating
n = userNum.length(); // Gets the sum of the string length in bytes
}
for (i = 0; i < n; i++) // Starting from 0, until num is less than the length of userNum go up one increment
sum = (sum + (userNum[i] - '0'));
if (sum % 9 == 0) {
cout << "Your number, " << userNum << " adds up to " << sum << " which is evenly divisible to 9" << endl << endl;
}
else {
cout << "Your number, " << userNum << " adds up to " << sum << " which IS NOT evenly divisible to 9" << endl << endl;
}
// Restart option begins here.
cout << "Would you like to try another number? (Y/N): ";
cin >> userInput;
if (userInput == 'y' || userInput == 'Y') {
goto tryagain;
cout << endl;
}
else if (userInput == 'n' || userInput == 'N') {
cout << endl;
cout << "Goodbye!" << endl;
}
else {
cout << endl;
cout << "You have entered an unknown input, goodbye!" << endl;
}
system("pause");
return 0;
}
答案 0 :(得分:4)
您可以通过3种方式进行此操作:
使用带std::regex_match的正则表达式。您要查找的正则表达式为^[0-9]+$
,它检查字符串是否仅包含数字。这是一个可能表达式,但不是绝对的
使用std::stoi尝试将字符串转换为整数。确保用try-catch
语句将其括起来,以确保可以正确解析该字符串。如果收到异常,则字符串不是数字。
正如Justin在评论中指出的那样,您也可以尝试使用std::isdigit并遍历字符串以检查每个字符。
依靠std
函数并尝试避免自己编写代码。第二种方法可能是您正在寻找的方法,因为如果该方法没有引发异常,则可以检查数字范围。
遇到无法解决的问题时,请查看list of algorithms on cppreference。尽可能多地使用它们,因为它们使代码更短,更易于阅读,并且可以为您带来好处(某些算法在最后只需要一个额外的参数即可开始并行执行该方法! )
答案 1 :(得分:0)
for (i = 0; i < n; i++) {
if(userNum[i] < '0' || userNum[i] > '9') {
cout << "Nah..";
return 0;
}
sum = (sum + (userNum[i] - '0'));
}
答案 2 :(得分:0)
给出必要的范围检查,范围为1 ... 1000,将数字和相加后得到的结果除以9不会得到任何好处,而不仅仅是在数字本身。因此,数字部分可以是简单的i < 1
,i > 1000
,i % 9 == 0
检查,其中i
是已解析的整数。
std::stoi
解析字符串开头的数字,因此尽管可以使用它,但它不会代替检查字符。例如1
,1.2
,1a
都被解析为1。您需要一个不以数字开头的字符串,只有这样,您才能获得std::invalid_argument
异常。因此,您也需要@Justin的建议,尽管在这里我将其翻转为“ any_of
,字符为not a digit
:
std::string s;
std::cin >> s;
try {
if (std::any_of(s.begin(), s.end(), [](char c) { return !std::isdigit(c); }))
throw std::invalid_argument(" is not all-digits");
int i = std::stoi(s);
if (i < 1) throw std::invalid_argument(" is less than 1");
if (i > 1000) throw std::invalid_argument(" is more than 1000");
if (i % 9 == 0) std::cout << i << " is divisible by 9" << std::endl;
else std::cout << i << " is not divisible by 9" << std::endl;
}
catch (std::invalid_argument ia) {
std::cout << s << ia.what() << std::endl;
}
catch (std::out_of_range oor) {
std::cout << s << " sure is not between 1 and 1000";
}
({stoi
的“自己的” invalid_argument
在这里不能出现,因为需要对所有数字进行预检查)
std::string s;
std::cin >> s;
std::string::iterator it = s.begin(), end = s.end();
int len = end - it;
try {
// if(len==0) will not happen because of the cin
if (*it == '0') {
if (len == 1) throw std::invalid_argument(" is less than 1");
throw std::invalid_argument(" starts with 0, multi-digit numbers do not do that");
}
if (len > 11) throw std::invalid_argument(" is too long for not being greater than 10^10");
if (len == 11) {
if (*it != '1'
|| std::any_of(it + 1, end, [](char c) { return c != '0'; }))
throw std::invalid_argument(" is greater than 10^10 or is not all-digits");
// well, here we know that the number is 10^10 (and is not divisible by 9)
// so this could return early
}
int sum = 0;
for (; it != end; ++it) {
if(!std::isdigit(*it)) throw std::invalid_argument(" is not all-digits");
sum += (*it) - '0';
}
if (sum % 9 == 0)std::cout << s << " is divisible by 9" << std::endl;
else std::cout << s << " is not divisible by 9" << std::endl;
}
catch (std::invalid_argument ia) {
std::cout << s << ia.what() << std::endl;
}