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;
}

私のコードからわかるように、検証スクリプトはうまく機能しています。解決したいバグがいくつかあります。

1-数値を入力すると、コードは正常に実行されますが、表示されます

Could not parse 'inputted number' as a number

明らかに、数字が入力されたとき、あなたはこれが起こらないようにしたいのです!

2-エラーメッセージは、入力した番号を[0]と表示しています。これは整数の使用と関係がありますか?これはどのように修正できますか?

ありがとう!

4

1 に答える 1

2

あなたの問題は非常に単純です、あなたはこの行に小さな間違いがあります

else (isalnum(str[i]));

elseステートメントはセミコロンで終わり、実際には何もしません。以下のステートメントが毎回実行されます。

i++;
    { 
    cout << "We could not parse '" << input << "' as a number.\n";
    } 
于 2012-11-19T00:26:39.917 に答える