I'm currently learning C++ from a tutorial site and I've run into a problem. I wrote a function that takes user input for two integers and is supposed to output their sum. However, my code only prompts for one number and then doubles that input, skipping the second prompt entirely. I even copied the example code from the tutorial, but it behaved the same way. Could this be a problem with my machine or how C++ handles input buffers, or did I mess up my code?
3 Answers
It looks like there might be an issue with how you're capturing input. The second cin might be picking up a newline left in the buffer from your first input, so it doesn’t prompt again. It might be worth checking how your input is being handled.
Your code seems to work fine on my end. I ran it, and it prompted for two numbers just like it's supposed to. To troubleshoot, try running your program directly from the command line instead of double-clicking the exe. That might help figure out if the issue is related to buffering.
I've seen similar problems when using certain compilers, especially MinGW. If you're running Windows, consider switching to Visual Studio instead. It's more straightforward, especially for beginners. Your output also needs a newline character at the end to avoid the weird terminal behavior you're seeing after the first run.
That's a great tip! I switched to Visual Studio and found it a lot easier to work with than MinGW.

Thanks for the suggestion! I've been meaning to try Visual Studio, so I'll give that a shot.