I'm having trouble with this section my code and am hoping that you can help me diagnose and solve the problem. I'm not sure if it makes a difference, but I compile and run my code in Cygwin.
The problem:
At run time when the program asks for the user input I can input somethimg like: "Guybrush Threepwood Throe" and the code will run just fine, but if I type the same thing and then use the left arrow button to move the cursor back over the input to make a correction (for instance to change the third word of the input from "Throe" to "Three"), the program will ignore (or seem to ignore) all of my "cin.ignore(1000, '\n');" and "cin.getline(input1, 31, '\n');" commands. I'm aware that you're not technically supposed to use the arrow keys in Cygwin, but I'm trying to make my code fool-proof.
Why does this happen, and assuming the user will eventually use those arrow keys, how can I prevent the undesired side-effects?
#include <cstdlib>
#include <iostream>
using namespace std;
int main ()
{
char input1[31];
char default1[31] = " ";
int success = 0;
int tries = 0;
while ((success != 0) && (tries < 4))
{
cout << "[By what name shall you be known? Max: 30 Characters]" << endl;
cout << ":" << endl;
// Point of possible conflict follows immediately
cin.getline(input1, 31, '\n');
if (((input1[0] > 64) && (input1[0] < 91)) || ((input1[0] > 96) && (input1[0] < 123)))
success = 1; // Success = 1 if input1 starts with a letter.
else
{
cout << "What kind of name doesn't begin with a letter?" << endl;
cout << "(Type a name that starts with a letter.)" << endl;
tries++;
}
}
if (tries >= 4)
{
char guybrush[31] = "Guybrush";
strcpy (input1, guybrush);
cout << "Fine. I'm going to assume your name is Guybrush." << endl;
}
cout << "Some text here..." << endl;
cin.ignore(1000, '\n'); // Used as a pause function
cout << "Some more text here..." << endl;
cin.ignore(1000, '\n');
strcyp (input1, default1);
cout << "Input command." << endl;
cout << ":";
cin.getline(input1, 31, '\n');
return 0;
}
Thank you for your input.