• What is the issue? ( Tap Code Program )
    2 replies, posted
[SOLVED] Tap Code is a method of communication by using taps or knocks to signify certain letters. You tap a certain number of times to signify what row the letter is in, then after a pause tap to signify the column. [IMG]https://fsmedia.imgix.net/ef/0e/37/f8/5ece/4811/8236/9dbff227bc37/tapcodejpg.jpeg?dpr=2&auto=format,compress,enhance&q=75[/IMG] I am attempting to make a program that allows me to tap keys on my keyboard to use as taps for " Tap Code ". For Example: I tap any key 4 times to get to the 4th row , then press enter to lock in the row number. Tap 3 times to get the 3rd Column, press enter to finish. At this point the problem is xTaps and yTaps are not increasing every time I press a key. They do sometimes, but not always. I should only have to press " Enter " twice for the code to finish and give me the output, but it will occasionally wait for me to press a third time and the output is never what it should be. #include <iostream> #include <string> char getTap() { char letter; int xTaps, yTaps; xTaps = -1; yTaps = -1; char tapCode[5][5] = { { 'a','b','c','d','e' },{ 'f','g','h','i','j' },{ 'l','m','n','o','p' },{ 'q','r','s','t','u' },{ 'v','w','x','y','z' } }; while (std::cin.get() != '\n') { std::cin.get(); xTaps++; } while (std::cin.get() != '\n') { std::cin.get(); yTaps++; } if (xTaps == -1) { xTaps = 0; } if (yTaps == -1) { yTaps = 0; } // These lines are to make sure it doesn't try to go to tapCode[-1][-1] which doesn't exist and causes problems letter = tapCode[xTaps][yTaps]; return letter; } int main() { char letter; letter = getTap(); std::cout << letter << '\n'; system("PAUSE"); return 0; } ( I am new to C++ and I am also having difficulty figuring out how to articulate the problem. I hope the way I am presenting the problem is understandable. I am using Microsoft Visual Studio 2015 on Windows 10 )
[QUOTE=Klespyrian;50427250][code] while (std::cin.get() != '\n') { std::cin.get(); xTaps++; } while (std::cin.get() != '\n') { std::cin.get(); yTaps++; } if (xTaps == -1) { xTaps = 0; } if (yTaps == -1) { yTaps = 0; } // These lines are to make sure it doesn't try to go to tapCode[-1][-1] which doesn't exist and causes problems [/code] [/QUOTE] It's been a while since I have had to deal with std::cin, but isn't cin always buffered so that you can only get one line at a time. So you have to call std::getline twice and count the number of characters returned. Something like this: [code] std::cout << "Enter knocks for row"; std::string taps; std::getline (std::cin, taps); yTaps = taps.length(); [/code] And then the same thing for column.
It worked! Thank you very much
Sorry, you need to Log In to post a reply to this thread.