【问题标题】:cin error in C++C ++中的cin错误
【发布时间】:2014-01-30 01:33:14
【问题描述】:

我正在编写一个程序,它将根据两个公式计算密码强度。它要求用户输入 2 个密码,一个不超过 8 个字符,另一个不超过 20 个字符。第一部分执行没有问题。但是,当我去执行第二部分时,同时出现输入密码和字符集的提示,当我输入任何内容时,无论是数字还是字符,它都会中止。我已经检查了好几次我的代码,但不明白为什么会这样。任何帮助将不胜感激!

int main()
{
    //All variables and constants are declared
    string eight_password, first_char, next_seven, twenty_password, first_char_twenty, next_seven_twenty, next_twelve, remaining;
    int ep_length, character_set, first_char_length, next_seven_length, character_set_20, twenty_length;
    double eight_ent_strength, eight_nist_strength, twenty_ent_strength;
    const int NIST_FIRST = 4, NIST_SEVEN = 2, NIST_REM = 1, NIST_CHARACTER=94, NIST_BONUS=6;
    const double NIST_TWELVE = 1.5; 

     //Console prompts for user to input password and character set
    cout << "Hello! Please enter a password of 8 characters or less (including spaces!):" << endl;
    getline(cin, eight_password);
    cout << "What character set is being used?";
    cin >> character_set>>ws;

    //Password length and information entropy strength are calculated and saved to the appropriate variables
    ep_length = eight_password.length();
    eight_ent_strength = (ep_length*((log(character_set))/(log(2))));

    //First character and next seven characters are extracted and saved to the appropriate variables
    first_char = eight_password.substr(0, 1);
    next_seven = eight_password.substr(1, 7);

    //First character and next seven characters lengths are calculated and saved to the appropriate variables
    first_char_length = first_char.length();
    next_seven_length = next_seven.length();

     //NIST strength is calculated and saved to the appropriate variable
    eight_nist_strength = (first_char_length*NIST_FIRST) + (next_seven_length*NIST_SEVEN)+((character_set/NIST_CHARACTER)*NIST_BONUS);

    //The information that was calculated is now printed back out on the console to be viewed by the user
    cout << "Your password " << eight_password << " is " << ep_length << " characters long. According to the information " << endl;
    cout<<"entropy formula, it has a strength of " << eight_ent_strength << "." << endl;
    cout << "The first character is \"" << first_char << "\" and the next seven characters are \"" << next_seven << "\". " << endl;
    cout << "According to the NIST formula, it has a strength of " << eight_nist_strength << "." << endl << endl;

    cout << "Now, please enter a password of 8 characters or less (including spaces!):" << endl;
    getline(cin, twenty_password);
    cout << "What character set is being used?";
    cin >> character_set_20;
    twenty_length = twenty_password.length();
    twenty_ent_strength = (twenty_length*((log(character_set_20)) / (log(2))));
    first_char_twenty = twenty_password.substr(0, 1);
    next_seven_twenty = twenty_password.substr(1, 7);
    next_twelve = twenty_password.substr(7, 19);
    remaining = twenty_password.substr(19);
    cout << remaining;
    return 0;

}

【问题讨论】:

  • 请从您的示例中删除所有无关代码。它使过程更加顺畅。
  • 你的问题是cin,而不是cout
  • 你建议如何解决这个问题?
  • using getline(cin, s) after cin 的可能重复项
  • 为什么您认为读取输入的问题是 cout 错误?

标签: c++ cin getline


【解决方案1】:

改变

cin >> character_set;

cin >> character_set;
cin.ignore( 1<<14, '\n' );

getline(cin, twenty_password) 的调用会消耗cin &gt;&gt; character_set; 的前一个换行符,这就是它不等待的原因。同样的问题和解决方案在这里:getline(cin, aString) receiving input without another enter

【讨论】:

  • 你说的是程序第一部分的cin&gt;&gt;character_set吗?我删掉的部分?
  • 当我这样做时,程序将无法运行。在我输入字符集并按回车后,它所做的就是转到下一行。它不会打印出信息。
  • @user3064203 在您的问题中显示更多使用cin 的代码。您在编辑帖子时取出了一些内容。
  • @user3064203 已更新为 cin.ignore1&lt;&lt;14 只是读取character_set 后忽略的任意大的最大字符数。
【解决方案2】:

在 2 个 getline() 函数之间尝试这个忽略函数,以消除缓冲区中的换行符

cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 

它在包含后工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多