A program I wrote calls for the input of a single alphabet letter and a single number. A problem I ran into when running the program was that I had to either press the space bar or enter after inputting the letter, in order for the program to observe the next input of a particular number. For example, if I type in a9 and hit enter, the program does nothing. But if I input like so, a 9 and then hit enter. It evaluates both, the string(which is one letter long) and the number.
What i wish to do is just have the program evaluate both variables by typing in a9 and hitting enter, instead of a 9 and hitting enter. So I scoured the internet looking for a way to limit what the program evaluates when inputting a letter that represents a declared string. By setting the width of the string input to 1 using setw, I am able to input a9 and have the program evaluate both symbols seperately and simultaneously(I assume).
Why does setting the setw(1) for the input of a particular string variable, cause the program to work like it does? Is this standard practice for a problem such as this? Some would probably say to use char instead of string. But I also needed to use find in the if expression to test the letter input against an array of alphabet letters.
1 2 3 4 5 6
cin >> setw(1) >> aLetter;; //Here I set the width of the string to 1 and input a letter.
cin >> aNumber; //Here I input a number.
When you entere a9'\n', that is a9 and ENTER cin>>setw(1) reduces the input to a width of 1. this input is 'a'.
After this, 9'\n' is still left in the buffer which is absorbed by the next call to cin