C ++/CLI -- CLR program using nested if statements

Source: Internet
Author: User

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 ;}
 


 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.