#getline function won't read input

22 messages · Page 1 of 1 (latest)

mellow summitBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

worldly eagle
#

!format

mellow summitBOT
#
int main() {
  // Main message:
  cout << "1. Encrypt message" << endl;
  cout << "2. Decrypt message" << endl;
  cout << "3. Show decrypted possibilities" << endl;
  cout << "4. Break code" << endl;
  cout << "Enter choice: ";

  int choice, shift, direction;
  string messege, encriptedMessege;
  cin >> choice;

  switch (choice) {
    case 1: {
      cout << "Enter shift (1-26): ";
      cin >> shift;

      cout << "Enter direction (f or b): ";
      cin >> direction;

      cin.ignore();
      cout << "Enter message: ";
      getline(cin, messege);

      encriptedMessege = encryptMessege(messege, shift, direction);
      cout << "Encrypted message: " << encriptedMessege;
      break;
    }
  }
}
oz
worldly eagle
#

!code

mellow summitBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
worldly eagle
#

Why do you have the cin.ignore() without arguments?

#

You’re close to solving the problem of getline getting skipped

shell scaffold
#

I didn't know it needed arguments

worldly eagle
#

Do you know what the function does?

shell scaffold
#

does it get rid of whitespace? im pretty sure thats my issue but i cant figure out how to fix it

#

the suggestions ive seen was use cin.ignore or use getline twice but neither worked for me

worldly eagle
#

It gets rid of more than just white spaces. It takes the size of the characters you mentioned or till it reaches a delimiter in the input stream and discards them.

#

A

#

By the curly bracket I mean the case 1: {} should just be case 1: \statements then break after the statements

shell scaffold
#

ok i got rid of the brackets and i tried cin.ignore(numeric_limits<streamsize>::max(), '\n') and it still wont read any input

#

it just skips it completely

worldly eagle
#

Move the cin.ignore(numeric_limits<streamsize>::max(), '\n'); right before the getline statement and compile it again

shell scaffold
#

ok yes it works now thank you

worldly eagle
#

Nice

#

I found this from stackoverflow and I think it describes it better than I did. “for std::cin statements you use ignore before you do a getline call, because when a user inputs something with std::cin, they hit enter and a '\n' char gets into the cin buffer. Then if you use getline, it gets the newline char instead of the string you want.”

mellow summitBOT
#

@shell scaffold Has your question been resolved? If so, type !solved :)

#

@shell scaffold

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.