我创建了一个使用char和int映射的函数,这样当一个数字作为字符串传递时,我可以检查每个字符以确保它是一个int.然后我接受该字符串并将其转换为纯int.出于某种原因,当它检查'0'时,它会在找到时抛出错误.我知道有更好的方法可以做到这一点,我已经在我的大型计划中实施了许多方法.对于正在发生的事情,这是一个纯粹的学术问题.
main.cpp中
#include <iostream>
#include <string>
#include <map>
class Logic {
public:
Logic();
std::map <char, int> IsNumber;
bool CheckForInterger(std::string);
int StringToInt(std::string);
};
Logic::Logic() {
IsNumber = { { '0',0 },{ '1',1 },{ '2',2 },{ '3',3 },{ '4',4 },{ '5',5 },
{ '6',6 },{ '7',7 },{ '8',8 },{ '9',9 } };
}
bool Logic::CheckForInterger(std::string word) {
for (char character : word) {
if (IsNumber[character]) {
//do nothing and check next char
}
else {
std::cout << "\n\"" << character << "\" …Run Code Online (Sandbox Code Playgroud)