G ++ compile the program to import the getch () function, getch

Source: Internet
Author: User

G ++ compile the program to import the getch () function, getch

Note: In ubuntu, G ++ compiles the C ++ program to import the getch () function (the program will continue to run only after a value is input by the standard function, no <conio. h> function library, so you need to import <curses. h> and use g ++-o test-lncurses test. cpp compilation, if the error still persists, use sudo apt-get install libncurses5-dev to install the header file.


C language getch () Problem

Getch (); is not a function in Standard C and does not exist in C .!!
Header file: conio. h
We recommend that you replace it with getchar () or another type.

Getch ()
Getch ():
Header file: conio. h
Function purpose: Read a character from the console, but it is not displayed on the screen.
Function prototype: int getch (void)
Returned value: read characters
For example:
Char ch; or int ch;
Getch (); or ch = getch ();
Use getch (); wait for you to press any key, and then continue to execute the following statement;
Use ch = getch (); after you press any key, assign the ASCII code corresponding to the key character to ch, and then execute the following statement.
Easy to error: 1. the header file is conio. h. Instead of stdio. h.
2. Call initscr () before use, and call endwin () at the end (). Otherwise, the function does not contain any characters.
The returned results.
Getch (); is not a function in Standard C and does not exist in C. So pay attention to the portability of the program when using it. Chinese C language beginners often use getch (); to pause the program and do not know the function source. We recommend that you use getchar (); (if possible) to replace this function or change a compiler.

When the C program is running, the program will flash through and add getch ();

Different functions used by compilers are different.
The landlord can use system ("pause"); instead of getch ();

# Include <stdio. h>

Int main ()
{
Int n, I, sum;

Printf ("enter a natural number :");
Scanf ("% d", & n );

For (I = 1, sum = 0; I <= n; I ++)
Sum + = I;
Printf ("sum of consecutive natural numbers from 1 to % d: % d \ n", n, sum );
System ("pause ");
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.