字符串比较问题 [英] String Comparision Problem

查看:73
本文介绍了字符串比较问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



请帮帮我,我正在尝试输入密码字段,并在用户输入密码后,我希望将其与存储的密码匹配。如果输入密码是相同然后它调用下一个函数。



问题

此程序从用户获取密码,直到它enter.after输入它与商店密码比较,但它与存储的密码不匹配,但我输入正确的密码,总是给我错误密码不匹配请帮助我,如果有人知道这个。



Hello Everyone!

Please Help me i am trying to make password Field and after taking password from user i want to match it with Stored password.if Enter password is Same then it call the next function.

Problem
this program take password from the user until it enter.after enter it compare with store password but it did not match the stored password but i enter the right password and always give me error "password is not match" please help me if any one know about this.

    bool Choice;
    char Con;
    while(Choice!=true)
    {
        Header();
        cout<<setw(45)<<"Enter Name:";
        cin>>Name;
        cout<<endl<<endl<<endl;
        cout<<setw(49)<<"Enter Password:";
        while(Con!=13)
        {
            Con=getch();
            Password+=Con;
            cout<<"*";
        }
        if(Password.compare("Qasim_365")==0)
        {
        cout<<endl<<endl<<endl
            <<setw(56)<<"Account Access Granted."<<endl;
        system("pause");
        system("cls");
//Function Atfter Password Same
        AdminMenu();
        break;
        }
        else
        {
            Con=0;
            cout<<"\n\n\t\t\t\tPassword Not Matched Try Again."<<endl;
            system("pause");
            system("cls");
            Choice=false;
        }
    }

推荐答案

纠正我,如果我错了(我没有在FOREVER中使用过char ),但他不是通过一个字母密码然后用下一个循环重写它?



Correct me if I'm wrong ( I haven't used char in FOREVER ), but isn't he passing a single letter to password then rewriting it with the next loop?

char con;  <small>Single Charactor</small>
while(Con!=13) <small>While Con != CR</small>
        {
            Con=getch();
            Password+=Con;  //Not sure how Password is defined but doesn't seem appropriate for adding to a string
            cout<<"*";
        }
int i = 0;
while(Con!=13){
     Con=getch();
     Password[i] = Con;
     cout<<"*";
     i++;
}
     Password[i] = '\0'





你可以使用GetLine()。< br $> b $ b



You could use GetLine().

std::string Password;
  std::cout << "Please, enter your password: ";
  std::getline (std::cin,password);
password.compare("Quasim...........





虽然我假设你在做单个字符,所以你可以用'*'替换它。


使用系统调用也不是一个好主意.Windows有一个sleep()函数可以暂停清除屏幕有点困难



Although I am assuming your doing single character so you can replace it with a '*'

Also it's never really a good idea to use system calls. Windows has a sleep() function to pause and Clearing the screen is a little more difficult

#include <windows.h>
#include <iostream>

int main(int argc, TCHAR argv[])
{

    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);

}

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

    // Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
      return;
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten ))
      return;

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
      return;

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten ))
      return;

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}</iostream></windows.h>





http://www.cplusplus.com/forum/beginner/1988/3/#msg10830 [ ^ 再次Windows



http://www.cplusplus.com/forum/beginner/43683/ [ ^ ]



显示如何隐藏文本以便使用getline()。请记住,谷歌是每个人最好的朋友,因为你会遇到的每一个问题,一个人已经做过的好机会,并发布了一个解决方案。



http://www.cplusplus.com/forum/beginner/1988/3/#msg10830[^] Again Windows

http://www.cplusplus.com/forum/beginner/43683/[^]

Shows how to hide your text so you could use getline(). Remember Google is everybodys best friend, because every problem you will encounter, good chance some one already did and posted a solution.


这篇关于字符串比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆