I'm making a quick program for my English class's movie that involves me hacking into a bank and stealing money (of course this doesn't actually hack anything), but I haven't programmed in C++ in a long time and I don't remember how to fix this problem here:
Code:
int main()
{
system ("TITLE BSP");
system ("COLOR 0A");
cout << "Credit Card Number: ";
getline(cin, number); // Either Here...
hackingSimulator(0, 10);
displayInformation();
}
void takeFunds()
{
int amount = 0;
char ch;
system("CLS");
cout << "Amount: ";
cin >> amount; // And/Or Here
funds += amount;
hackingSimulator(7, 10);
displayInformation();
}
When I input anything in for number, such as:
Code:
1
1234
1234 1234 1234 1234 // Similar to what will be entered in the movie (with spaces included)
...it will also show up for amount and will not give me a prompt to enter any value. Does anyone know how to fix this? I've tried researching it by looking up a cin reference guide and Googling it, and I found some similar problems but no solutions that worked for me. I'm not sure if I'm missing something in the reference or if the problem is something else entirely. Any and all help will be appreciated, thanks in advance.
PS: Here's the full program if you need it:
(Please excuse my lazy programming, but this is all basically a dummy program with no real function other than being filmed which is why I used a lot of shortcuts like the goto command, and also why I have a lot of menu options but few menu functions)
Nasty! The problem you've got is nothing to do with the c++ I/O system but the fact that you are trying to combine two different input systems - c++ cin and Windows input using GetkeyState() and the two don't mix very easily!
When you press the up-arrow (or down arrow or any other special key) then GetkeyState correctly tells the state of the tested key BUT the key still stays in the input buffer and is not processed. If this were a windows program, then windows would extract the key from the input buffer and send messages. But as this is not a windows progam, it doesn't. So up-arrow and down-arrow etc are still in the input buffer and then you do a cin >> to get input from the console. cin finds the arrows and treats them like it would if you were using them at the command prompt ie goes back and forth through the input history. It then finds the CR and uses whatever is in the input buffer as input.
If you want to use special keys in a console program you have to do it another way. In your case this is easiest using the special console input function getch(). This will read the next character from the input WITHOUT performing any special processing for special characters. The special characters you use all produce 2 input characters 0xe0 followed by 0x48 for up and 0x50 for down. So your display menu loop could be something like
Code:
while (true) {
char ch = getch();
//If have special key code then get next char as they come in pairs
if (ch == 0xe0) {
ch = getch();
}
//if (GetKeyState(VK_UP) < 0)
if (ch == 0x48)
{
sel = (!sel ? 13 : (sel - 1));
goto _sel;
}
//if (GetKeyState(VK_DOWN) < 0)
if (ch == 0x50)
{
sel = ((sel == 13) ? 0 : (sel + 1));
goto _sel;
}
//if (GetKeyState(VK_RETURN) < 0)
if (ch == 0x0d)
break;
}
You'll need to insert for the definition of getch().
Nasty! The problem you've got is nothing to do with the c++ I/O system but the fact that you are trying to combine two different input systems - c++ cin and Windows input using GetkeyState() and the two don't mix very easily!
Now you sir, are my new best friend! I had no idea that was the issue, no wonder I couldn't get anything else to work! Thank you so much, the program works perfectly now and the movie shall continue
Bookmarks