c++ - C++密码程序

标签 c++

密码程序不工作....请帮助....正确输入也说密码错误

#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();
}

我想知道为什么这个密码程序不起作用....他们还有其他方法吗

最佳答案

为了能够使用 strcmp(),您需要以 NUL 终止 pass。您还需要确保 pass 足够大以容纳 NUL。

关于c++ - C++密码程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14170154/

相关文章:

c++ - 链表 : Difference between "node* head=new node" and "node* head"

c++ - 可变大小指针数组

c++ - 如何使用 gdb 调试使用 yasm 组装的代码?

c++ - 在 C++ 中初始化和销毁​​二维 vector

c++ - 在 C++ 中传递对要分配给指针的函数的引用,哪种方式更好?

c++ - 用于默认模板参数的参数包

C++:一种在分隔变量定义和测试的 if 语句中声明一个变量(或多个变量)的方法?

c++ - 使用一个循环与两个循环

c++ - boost gzip_compressor 和 gzip_decompressor

javascript - 从 C++ 调用 javascript 函数