ошибка cin в C ++

0

Я пишу программу, которая будет вычислять силу пароля в соответствии с двумя формулами. Он требует, чтобы пользователь вводил 2 пароля, один из которых составлял восемь или менее символов, а другой - 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;

}
  • 1
    Пожалуйста, удалите весь посторонний код из вашего примера. Это делает процесс намного более плавным.
  • 0
    Ваша проблема с cin , а не с cout .
Показать ещё 4 комментария
Теги:
cin
getline

2 ответа

0

попробуйте эту функцию игнорирования между двумя функциями getline(), чтобы избавиться от новой строки в буфере

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

Он работает после этого включения

  • 0
    Этот ответ работает на вас?
0

+ Изменить

cin >> character_set;

в

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

Вызов getline(cin, twenty_password) потребляет предыдущую новую getline(cin, twenty_password) от cin >> character_set; поэтому он не ждет. Эта же проблема и решение здесь: getline (cin, aString), принимающий вход без другого ввода

  • 0
    Вы говорите о cin>>character_set в первой части программы? Часть, которую я отредактировал?
  • 0
    @ user3064203 да
Показать ещё 4 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню