0
#include <iostream>
#include <cstring>
using namespace std;

void getInput(char *password, int length)
{
    cout << "Enter password: ";
    cin >> *password;
}
int countCharacters(char* password)
{
    int index = 0;
    while (password[index] != "\0")
    {
        index++;
    }
    return index;
}
int main()
{
    char password[];
    getInput(password,7);
    cout << password;
    return 0;
}

やあ!ここで、atm で実行できない 2 つのことを試しています。main で長さが指定されていない char 配列を作成しようとしており、関数 countCharacters で char 配列の単語数をカウントしようとしています。しかし、パスワード [インデックス] は機能しません。

編集:宿題をしているので、cstringsのみを使用する必要があります。EDIT2: また、「strlen」関数の使用も許可されていません。

4

1 に答える 1

1

まずは交換

char password[];

char password[1000]; // Replace 1000 by any maximum limit you want

そして、次のように置き換えます。

cin >> *password;

cin >> password;

また、代わりに"\0"置く必要があります'\0'

PS C++ には長さが指定されていない char 配列はありません。代わりに std::string を使用する必要があります ( http://www.cplusplus.com/reference/string/string/ ):

#include <string>

int main() {
    std::string password;
    cin >> password;
    cout << password;
    return 0;
}
于 2013-03-19T10:34:41.490 に答える