Keystroke carry-over issue

Started by
2 comments, last by Kippesoep 16 years ago
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; }
BladeStoneOwner, WolfCrown.com
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
BladeStoneOwner, WolfCrown.com
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)oWhat is your character's name?DaveDo you want to be called (Y/N): Dave

Do you get something different? Or is that not what you want?
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.
Kippesoep

This topic is closed to new replies.

Advertisement