Why this getline function is not working in code? | Sololearn: Learn to code for FREE!


Why this getline function is not working in code?

I was making a code of vigenere cryptography and this getline function is not working. Copy the code into your IDE then try. I am using Codeblock. https://code.sololearn.com/cZmdbO8R381L/?ref=app

2/2/2020 9:03:46 AM

Abhishek Dimri

2 Answers

New Answer


Can you give me sample string for <key> and <message> for testing? also the expected result string? As I see it, you mix up C and C++ here, looking at how you measure the length of <key> for example. But I see you already know C++ string has `length` method which does the job.


The call to std::getline() will result in an empty string because the newline character from your prior input is still in the input buffer. std::getline() will encounter that newline character, causing it to stop immediately after extracting and discarding the newline character because it acts as a delimiter. You can discard such leading whitespace characters with a call to std::ws: https://en.cppreference.com/w/cpp/io/manip/ws Just change std::getline( std::cin, message ); to std::getline( std::cin >> std::ws, message); and you should be fine. I didn't look for any other errors right now, but this should get you started.