C++我的程序将退格键读取为字符



我正在使用一个 c++ 程序,但我遇到了烦人的错误。错误是当我输入密码时,它会将退格键计为一个字符,所以我可以修复它吗?这是代码。

#include <iostream>
#include <conio.h>
using namespace std;
int main() {
  string password, username, lon, nu, np;
  char c;
  int StarNum = 0, humanproof;
  cout << "Do you wanna create a user or login?";
  cout << "nLogin" << endl;
  cout << "New" << endl;
  cin >> lon;
  if(lon=="login"){
    goto login;
  }
  if(lon=="new"){
    goto newa;
  }
  login:
  cout << "Username: ";
  cin >> username;
  lol:
  cout << "Password: ";
  while (c != 13)
  {
    c = (char)getch();
    if(c == 13) break;
    StarNum++;
    password += c;
    cout << "*";
    if (c == 127 || c == 8){
      //go here to fix the problem
    }
    password = "";
    goto lol;
  }
}
if(username == "user" && password == "pass" || username == nu && password == np){
  cout << "nYou are logged in.";
  goto options;
} else {
  cout << "nusername or password is wrong" << endl;
  return 0;
}
return 0;
newa:
cout << "Username:";
cin >> nu;
cout << "password:";
cin >> np;
cout << "Type the number fourhoundred and twentie three too proof you are a human: ";
cin >> humanproof;
if(humanproof == 423){
  cout << "The username is " << nu << endl;
  for(int no = 0; no <= 100; no++){
      cout << endl;
  }
  goto login;
  return 0;
}
if(humanproof!=423){
  cout << "wrong answer!";
  return 0;
}
options:
    int op;
    cout << "nwhat do you want to do?" << endl;
    cout << "1. Calculator" << endl;
    cout << "2. About"<< endl;
    cout << "3. Just for fun" << endl;
    cout << "4. Exit" << endl;
    cin >> op;
    if(op==1){
        goto calculator;
    }
    if(op==2){
        goto info;
    }
    if(op==3){
        goto fun;
    }
    if(op==4){
        return 0;
    }
    else{
        cout << "you entered a invalid number. " << endl;
        return 0;
    }
calculator:
double n1, n2, sum;
int opa;
cout << "Choose a operation" << endl;
cout << "1. Addition/+" << endl;
cout << "2. Subscraction/-" << endl;
cout << "3. Multiplication/x" << endl;
cout << "4. Divsion/ /" << endl;
cin >> opa;
if(opa == 1){
    cout << "enter number 1" << endl;
    cin >> n1;
    cout << "enter number 2" << endl;
    cin >> n2;
    sum = n1 + n2;
    cout << "the sum is " << sum;
    return 0;
}
if(opa == 2){
    cout << "enter number 1" << endl;
    cin >> n1;
    cout << "enter number 2" << endl;
    cin >> n2;
    sum = n1 - n2;
    cout << "the sum is " << sum;
    return 0;
}
if(opa == 3){
    cout << "enter number 1" << endl;
    cin >> n1;
    cout << "enter number 2" << endl;
    cin >> n2;
    sum = n1 * n2;
    cout << "the sum is " << sum;
    return 0;
}
if(opa == 4){
    cout << "enter number 1" << endl;
    cin >> n1;
    cout << "enter number 2" << endl;
    cin >> n2;
    sum = n1 / n2;
    cout << "the sum is " << sum;
    return 0;
}
if(opa > 4){
  cout << "You entered a invalid number";
  goto calculator;
}
info:
    cout << "Created by Bergur 2013";
    return 0;
fun:
    cout << "You want an eyepad(ipad)?";
}

您的代码已经检查:

while (c != 13)

并防止处理换行符,执行类似于您需要的退格字符,其编号为 8

要解决您的问题,请先:

StarNum++;

加:

if (c == 8 && StarNum > 0) {
 StarNum--;
 if (password.size () > 0) 
  password.resize (password.size () - 1);
 continue;
}

请格式化您的代码。此外,尝试提供重现问题的最小代码,而不是整个代码。

与您的问题无关

  • 尽量不要使用goto。
  • 不要使用 ASCII 值,而是使用 char 文字,即使用""而不是 13。

此外,您可以简单地在while中添加一个其他条件:

while (c != 'n' && c != ' ')

最新更新