Jump to content
  • Advertisement
Sign in to follow this  
Woodlander

C++ Console

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been messing around in C++, writing programs in console, and I often find that I try doing something I could do in BASIC, but cannot seem to do in C++. Is there any way I can sortof...'Lock' the Input? I use cin to ask for player input, but if you just press enter with no input, it'll do like it does in DOS, the input Cursor will just keep going down the screen until you type something in. I tried adding an if (pInput = "") etc But obviously that didn't work It's no big deal if it's not possible (In basic the variable would just be returned as null, whereas in C++ it seems to not even bother with the Input if nothing is there) but it'd be nice since I'm trying to keep my Console window orderly and such. Thanks in advance =)

Share this post


Link to post
Share on other sites
Advertisement
I'm assuming "pInput" is a char*, right? When you do the if(pInput=="") test, you're comparing two char*'s.. not the strings. What you'd need to do is something like "if(0 == strcmp(pInput,""))" or maybe:


char pInput[128];
// Loop until a non-empty string is entered. (pressing [enter] on a blank line
// will advance the cursor downwards for us.
while(gets(pInput)[0]=='\0')
{
}

Share this post


Link to post
Share on other sites
#include <iostream>
#include <string>

int main()
{
char a;
std::string str;
a = std::cin.get();
std::getline( std::cin, str );
return 0;
}


std::getline will put the input into the second argument, an std::string. Don't know if you can send it a char array. std::cin.get() returns a single char.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!