Why Does My C++ Code Only Prompt for One Input Instead of Two?

0
10
Asked By SillyPineapple42 On

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

Answered By DebuggingDude On

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.

Answered By CuriousCoder99 On

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.

Answered By TechWiz123 On

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.

SillyPineapple42 -

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

CodingNinja42 -

That's a great tip! I switched to Visual Studio and found it a lot easier to work with than MinGW.

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.