C++ C++;数字验证-正在使用循环

C++ C++;数字验证-正在使用循环,c++,for-loop,validation,C++,For Loop,Validation,我发布了一个早期的问题,要求帮助在代码中使用验证脚本。在回答了一个非常有用的问题后,我大致上能够弄明白我需要如何继续下去。嗯,我遇到了一个很大的障碍 #include <iostream> #include <sstream> #include <string> #include <ctype.h> using namespace std; int main() { unsigned __int64 input = 0;

我发布了一个早期的问题,要求帮助在代码中使用验证脚本。在回答了一个非常有用的问题后,我大致上能够弄明白我需要如何继续下去。嗯,我遇到了一个很大的障碍

#include <iostream>
#include <sstream>
#include <string>
#include <ctype.h>

using namespace std;

int main()
{
        unsigned __int64 input = 0;
        int n, i;
        char str[]="c3po...";
        i=1;
        n=0;


        for (cout << "Input a number" << endl; cin >> input; cin.ignore(numeric_limits<int>::max(), '\n'))
    {
        cout << "We're parsing your input '" << input << "'\n";

     if (input % 2 == 0) 
        {
            cout << "Even!" << endl;
        }

    else if (input % 2 == 1)
        {
            cout << "Odd" << endl;
            cout << "Lets make it even shall we? " << "Your new number is... " << input + 1 << endl;
        }
    else (isalnum(str[i]));i++;
        { 
        cout << "We could not parse '" << input << "' as a number.\n";
        }    
    }
    system ("pause");
    return 0;
}
显然,当输入一个数字时,你不希望发生这种情况

2-对于错误消息,它将输入的数字显示为[0]。这与使用整数有关吗?如何解决这个问题


谢谢

你的问题很简单,你在这方面有点小错误

else (isalnum(str[i]));
else语句以分号结尾,实际上什么也不做。每次都将执行以下语句

i++;
    { 
    cout << "We could not parse '" << input << "' as a number.\n";
    } 
i++;
{ 

对不起,伙计们,我现在可以睡觉了。明天早些时候开始。你没有实践上一个问题。你仍然直接在输入中提取,而不是使用变量来验证用户的输入。我无法理解该代码。我将其分解并更改了布局,使其看起来像我将如何编码,但我没有不知道这些部分做了什么:(我保留了它,所以当我获得更多的经验时,我会回去修改它。然后使用常规循环或完全远程重写它。基本上有两个步骤:1)获取用户输入,2)验证它。我可以指出两个问题:a)您使用的是isalnum()而不是isalpha()或!isdigit();b)您正在“输入”中直接阅读它的类型是uuu int64,因此它只能包含一个数字。当然,还有一个else-thing。嗯,我该如何解决这个问题呢?我想你可以删除它。else后面的语句只检查str中字符串的第二个字母,因为str和I没有变化。因为没有if语句,每次都会打印错误消息。if你想检查用户输入是否是一个数字签出,有没有办法用自己的代码来排序?我已经很久没有做C++了,所以可以尝试掌握基本知识,你发送给我的链接似乎是直接在我的头上!如果你的变量是无符号整数,那么验证它是没有意义的,因为它会无论用户输入的是好是坏,都要包含一个有效的数字。进行真正的验证需要更复杂的代码,就像链接中的代码一样。
i++;
    { 
    cout << "We could not parse '" << input << "' as a number.\n";
    }