0

パスワードプログラムが機能しない....plsヘルプ....正しい入力の場合も間違ったパスワードが表示されます

#include<stdio.h>
#include<conio.h> 
#include<string.h>
#include<iostream.h>

void main()
{  
  clrscr();
  int ctr=0;
  int  o;
  char pass[5];

  cout<<"enter password";
  for(int i=0;i<5 && (o=getch())!=13  ;i++)
  {  
    pass[i]=o;

    putch('*');
  }

  ctr=strcmp(pass,"luck");
  cout<<ctr;
  if(ctr==0)
  {
    cout<<"welcome";
  }
  else
  {
    cout<<"wrong password";
  }
  getch();
}

このパスワードプログラムが機能しない理由を知りたいのですが...他の方法です

4

2 に答える 2

6

を使用できるようにするにはstrcmp()、NULで終了する必要がありますpasspassまた、NULを収容するのに十分な大きさであることを確認する必要があります。

于 2013-01-05T08:48:05.847 に答える
0

使用中なので<conio.h>、Windowsが使用されていると想定しています。興味のある方は、これを行うための適切な方法をここから始めてください。Enter キーを押すと終了する行をパスワードとして入力し、アスタリスクを表示しません。長さが簡単にわかるからです。

//stop echoing input completely
HANDLE inHandle = GetStdHandle(STD_INPUT_HANDLE); //get handle to input buffer
DWORD mode; //holds the console mode
GetConsoleMode(inHandle, &mode); //get the current console mode
SetConsoleMode(inHandle, mode & ~ENABLE_ECHO_INPUT); //disable echoing input

//read the password
std::string password; //holds our password
std::getline(std::cin, password); //reads a line from standard input to password

//compare it with the correct password
std::cout << (password == "luck" ? "Correct!\n" : "Wrong!\n"); //output result

//return console to original state
SetConsoleMode(inHandle, mode); //set the mode back to what it was when we got it

もちろん、それを改善するためにできることはあります (ハードコードされたパスワード文字列は決して良いことではありません) 必要に応じて先に進んでください。フォローする構造。一度に 1 文字ずつ行って C の文字列やコードに頼るのではなく、パスワード入力を取得するときに好きなものを使用することができます。

于 2013-01-05T09:11:15.080 に答える