Jump to content
  • Advertisement
Sign in to follow this  
BladeStone

Keystroke carry-over issue

This topic is 3708 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

Quite simple, everytime a get array of characters comes after a get single character, the last key stroke is place on the line. See attached code for results. How do I fix this? // Author: BladeStone // Created 4/1/08 // Purpose: to fix the character carry over issue #include<iostream> #include<conio.h> int main(void) { // define var's char tempChar; char tempString[80]; int done = 0; // Start question std::cout << "Do you want to create a new character? (Y)es or (N)o\n"; do { tempChar = getch(); tempChar = toupper(tempChar); } while(tempChar!='Y' && tempChar !='N'); // test var if(tempChar=='N') return 0; // Main loop if(tempChar=='Y') { do { std::cout << "What is your character's name?\n"; // This is where the last choice appears! do { std::cout << std::flush; // doesn't work! std::cin >> tempString; std::cout << "Do you want to be called (Y/N): " << tempString << std::endl; tempChar = getch(); tempChar = toupper(tempChar); } while(tempChar !='Y' && tempChar!='N'); if(tempChar=='Y') done=1; } while(done!=1); } return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Just found a solution, but is seams very hackish.

Before the std::cin >> tempString;, I put in a getch(); and that will capture that one character which normally appears on the line when you need to enter your name. The std::flush; doesn't work. Is this an acceptable way to write the code?

Thank you in advance.
BladeStone

Share this post


Link to post
Share on other sites
I'm not sure I understand what your problem is.
I even compiled the code and ran it, I get the following:

Do you want to createa a new character? (Y)es or (N)o
What is your character's name?
Dave
Do you want to be called (Y/N): Dave

Do you get something different? Or is that not what you want?

Share this post


Link to post
Share on other sites
It also works for me, but that is dependent on which compiler and library you used to build it. You're mixing C style IO (getch) with C++ style IO (iostreams), which is not a very good idea. tbh, I find C++ style user IO an absolute nightmare, because of this and other nastiness.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!