79643122

Date: 2025-05-28 23:42:57
Score: 1
Natty:
Report link

My program is showing output but it for some reason it just skips the section "Please enter your User Name or your Official Name ". How can I fix this?

The reason your program skips is because it uses operator >> to extract the userid.

// From the OP:
char userid[100];
cin >> userid;

That operator only extracts one "word" at a time. By "word" I mean a non-empty sequence of charaters that does not contain a space or other whitespace character.

Extraction stops when a whitespace character is encountered, leaving the whitespace character in the stream. Note that the newline character ('\n') is a whitespace character. Extraction also stops if end-of-file file is encountered, but, in general, that is not an issue when using std::cin to get user input from the keyboard.

In your trial run, you entered two words for userid. Thus, "John Smith" was placed into the input stream. When you pressed Enter, that keystoke was also placed into the stream (as a newline). The stream contents at this point were "John Smith\n".

The statement cin >> userid; caused first word, "John", to be extracted, and stored in userid. The second word, "Smith", along with the space that precedes it, was left in the input stream.

The statement

cin.ignore();

extracts and discards a single character, in this case the space before "Smith."

The next input is read using std::getline.

string username;
getline(cin, username);

std::getline reads an entire line of characters, stopping either when a newline is encountered or when the stream hits end-of-file. If getline stops because it found a newline, it extracts, and discards, the newline.

Blank lines are okay, as are lines containing whitespace. getline happily extracts and stores whatever it finds.

In your program, getline extracted "Smith", and stored it in the string username. getline also extracted the the newline character that followed "Smith". It was discarded.

Note that all of this happens without any need for the console window to pause for user input. That's because keyboard input from cin is buffered. The only time cin causes the console window to pause, is when its buffer is empty. In this case, the buffer was not empty. It still contained "Smith\n". So getline read from the buffer, without pausing for keyboard input from the user.

From the outside, it may look like your program skipped username, but it did not. "Smith" was extracted from the buffer, and stored, in username.

After that, the buffer was empty. Thus, the program paused, as expected, to get keyboard input for passphrase.

char passphrase[300];
cin.getline(passphrase, 300);
Reasons:
  • Blacklisted phrase (0.5): How can I
  • Blacklisted phrase (1): How can I fix this
  • RegEx Blacklisted phrase (1.5): How can I fix this?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • High reputation (-1):
Posted by: tbxfreeware