0

I am writing a program that will calculate password strength according to two formulas. It requires the user to enter 2 passwords, one being eight characters or less and the other being 20 characters or more. The first parts executes with out problem. But, when I go to execute the second part, the prompt to enter the password and character set both show up at the same time and when I enter anything, whether it be numbers or characters, it aborts. I have checked over my code several times and don't understand why this is happening. Any help would be greatly appreciated!

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;

}
Joe
  • 41,484
  • 20
  • 104
  • 125
user3064203
  • 103
  • 1
  • 2
  • 7

2 Answers2

1

Change

cin >> character_set;

to

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

The call to getline(cin, twenty_password) consumes the previous newline leftover from cin >> character_set; which is why it doesn't wait. This same problem and solution is here: getline(cin, aString) receiving input without another enter

Community
  • 1
  • 1
Matt
  • 20,108
  • 1
  • 57
  • 70
  • Are you talking about the `cin>>character_set` in the first part of the program? The part that I edited out? – user3064203 Jan 30 '14 at 01:51
  • When I do that, the program won't run. After I enter the character set and press enter all it does is go to the next line. It doesn't print out the information. – user3064203 Jan 30 '14 at 01:57
  • @user3064203 Show more of the code that uses `cin` in your question. You took some of it out when you edited the post. – David G Jan 30 '14 at 02:13
  • @user3064203 Updated with `cin.ignore` instead. The `1<<14` is just some arbitrarily large maximum amount of characters to ignore after reading in `character_set`. – Matt Jan 30 '14 at 02:37
0

try this ignore function in between the 2 getline() functions to get-rid of the newline in the buffer

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

It works after this inclusion

Varo
  • 831
  • 1
  • 7
  • 16