【问题标题】:C++ my program reads backspace as a characterC++ 我的程序将退格读取为字符
【发布时间】:2014-01-16 17:40:27
【问题描述】:

我正在使用 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)?";
}

【问题讨论】:

标签: c++ passwords


【解决方案1】:

您的代码已经检查:

while (c != 13)

并防止换行符被处理,使用退格符执行类似于您需要的操作,其数量为 8

要解决您的问题,之前:

StarNum++;

添加:

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

【讨论】:

  • 是的,但如何删除它?
  • cout&lt;&lt;'\b'; 后跟cout&lt;&lt;' ';cout&lt;&lt;'\b'; 也是如此,它会返回一个字符,删除字符,然后再次返回
【解决方案2】:

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

与您的问题无关

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

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

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

【讨论】:

  • 更像从不使用goto
  • 是的,但是如何删除它?
  • 以及如何在不使用 goto 的情况下到达某个地方?
  • @user3119614 您需要重新设计整个程序才能使其在不使用gotos 的情况下正常工作。是的,这就是 gotos 的糟糕程度! :)
猜你喜欢
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多