C ++/CLI -- CLR program using nested if statements
// Nested ifCLR. cpp: Main project file. # Include "stdafx. h" using namespace System; int main (array
^ Args) {wchar_t letter; Console: WriteLine (L "Enter a letter:"); letter = Console: Read (); if (letter> = 'A ') if (letter <= 'Z') {Console: WriteLine (L "You entered a captial letter. "); return 0;} if (letter> = 'A') if (letter <= 'Z') {Console: WriteLine (L" You entered a small letter. "); return 0;} Console: WriteLine (L" You did not enter a letter. "); return 0 ;}
According to the Code in the book, the window disappears as soon as it is running.
However, when compiling the C ++/CLI Console application, it is found that even if Console: ReadLine (); is used, the runtime window still disappears.
I am going to summarize some suggestions on the Internet.
First, explain the buffer content.
Each input/output stream maintains a character buffer, which is used to receive input from the stream or to output to the stream. When the original program runs letter = Console: Read ();, an input prompt is displayed. If you input 's' and press enter, it is equivalent to inputting's \ r \ n ', the 'S' is read to the letter variable, and the buffer content will be '\ r \ n', that is, the Return key value is left. Console: ReadLine () reads one row at a time, that is, the Enter key at the end of the row can be Read; Console: Read (); Read one character at a time, you need to read '\ r \ n' twice to clear it to the buffer zone.
When the original program executes Console: ReadLine (), the buffer contains \ r \ n, it will read the first \ r \ n in the buffer and its content (null here), without waiting for user input, the user will not have the opportunity to enter. It's just a flash.
Here we will summarize the testing results of netizens (verified by myself ):
If You only add two consoles: Read (); on the Console: WriteLine (L "You did not enter a letter.");, the window disappears;
If three consoles: Read (); are added after the Console: WriteLine statement, the window is displayed;
If you add two consoles: Read (); and one Console: ReadLine (); after the Console: WriteLine statement, the window can also be displayed;
If two consoles: ReadLine (); are directly added after the Console: WriteLine statement, the window can also be displayed;
After the Console: WriteLine statement, add a Console: Read (); and a Console: ReadLine ();, and the window disappears.
Finally, paste the modified Code.
// Nested ifCLR. cpp: Main project file. # Include "stdafx. h" using namespace System; int main (array
^ Args) {wchar_t letter; Console: WriteLine (L "Enter a letter:"); letter = Console: Read (); if (letter> = 'A ') if (letter <= 'Z') {Console: WriteLine (L "You entered a captial letter. "); Console: ReadLine (); return 0;} if (letter> = 'A') if (letter <= 'Z ') {Console: WriteLine (L "You entered a small letter. "); Console: ReadLine (); return 0;} Console: WriteLine (L" You did not enter a letter. "); Console: ReadLine (); return 0 ;}