Need Help with User Input in C++

0
7
Asked By CodingNinja42 On

I've started learning C++ and was working on a program that asks for a user's age and name. The age section works fine, but when I try to get the user's name using getline(cin, name), it skips the input and just goes on without capturing anything. Can anyone help me understand what's going wrong? I appreciate any assistance!

2 Answers

Answered By TechGuru99 On

It sounds like you might be running into an issue with the input buffer. If you've used cin before getline, there could be a newline character left in the buffer that's causing getline to skip the input. Try checking out this explanation: [Stack Overflow Link Here]. Posting your actual code would help too!

CodingNinja42 -

Thanks, I didn't think to post the code here. I’ll take a look at that link!

TechGuru99 -

No problem! Let me know if that helps!

Answered By CodeWhiz88 On

Can you share your code? That'll make it easier to figure out what's going on!

CodingNinja42 -

Sure! Here's my code:

```cpp
string name;
cout << "Now, enter your name... NOW!!!!" << endl; cout << "---->";
getline(cin, name);
cout << "Really? " << name; cout << " is your name... a whole "; cout << name.length(); cout << " letters" << endl; cout << "weirdo... gtfo" ; ``` Hope this helps!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.