为什么我的代码不打印无效值来通知用户?



在这个程序中,我试图打印被程序识别为无效的单个值。例如,任何非数字值都是无效的,程序必须将其与定义的错误消息一起打印。我的代码:

#include <iostream>
using namespace std;
void Welcome();
void botVerification();
bool isNumber(string botV);
int main ()
{
Welcome();
botVerification();
botVerification();
botVerification();
return 0;
}
void Welcome()
{
cout << "Welcome to our program" << 'n';
}
void botVerification()
{
string botV;
cout << "Enter the number to start the Calculator: ";
cin >> botV;
if(isNumber(botV))
cout << "Success" << 'n';
else
cout << "Not success " << 'n';
}
bool isNumber(string botV)
{
int badIndex = 0;
for (int bcounter = 0; bcounter < botV.length(); bcounter++)
if (!(botV[bcounter] >=48 && botV[bcounter] <= 57))
{
return false;
badIndex = bcounter;
cout << "Not success " << botV[badIndex] << " is not a number "<< 'n';
break;
}
return true;
}

您的程序在到达"cout";在您的";bool";。

bool isNumber(string botV)
{
int badIndex = 0;
for (int bcounter = 0; bcounter < botV.length(); bcounter++)
if (!(botV[bcounter] >=48 && botV[bcounter] <= 57))
{
return false;
badIndex = bcounter;
cout << "Not success " << botV[badIndex] << " is not a number "<< 'n';
break;
}
return true;
}

修复

bool isNumber(string botV)
{
int badIndex = 0;
for (int bcounter = 0; bcounter < botV.length(); bcounter++)
if (!(botV[bcounter] >=48 && botV[bcounter] <= 57))
{
badIndex = bcounter;
cout << "Not success " << botV[badIndex] << " is not a number "<< 'n';
break;
return false;
}
return true;
}

最新更新