37

Cで「Press Any Key to Continue」として機能するvoid関数を作成するにはどうすればよいですか?

私がやりたいことは次のとおりです。

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
//The Void Function Here
//Then I will call the function that will start the game

Visual Studio 2012 でコンパイルしています。

4

5 に答える 5

51

Borland TURBO Cが MS-DOS/Windows 用にのみ提供する標準関数ではないため、getchar()代わりにC 標準ライブラリ関数を使用してください。getch()

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");  
getchar();    
 

ここでgetchar()は、リターン キーを押すprintf必要があるため、ステートメントはpress ENTER to continue. 別のキーを押しても、ENTER を押す必要があります。

printf("Let the Battle Begin!\n");
printf("Press ENTER key to Continue\n");  
getchar();    

Windowsを使用している場合は、使用できますgetch()

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();   
//if you press any character it will continue ,  
//but this is not a standard c function.

char ch;
printf("Let the Battle Begin!\n");
printf("Press ENTER key to Continue\n");    
//here also if you press any other key will wait till pressing ENTER
scanf("%c",&ch); //works as getchar() but here extra variable is required.      
于 2013-09-14T12:11:18.713 に答える
3

使用getch():

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();

Windows の代替手段は_getch()です。

Windows を使用している場合、これは完全な例です。

#include <conio.h>
#include <ctype.h>

int main( void )
{
    printf("Let the Battle Begin!\n");
    printf("Press Any Key to Continue\n");
    _getch();
}

PS @Rördが指摘したように、POSIXシステムを使用している場合は、cursesライブラリが正しくセットアップされていることを確認する必要があります。

于 2013-09-14T11:55:30.040 に答える
1

これを試して:-

printf("Let the Battle Begin!\n");
printf("Press Any Key to Continue\n");
getch();

getch()コンソールから文字を取得するために使用されますが、画面にはエコーしません。

于 2013-09-14T11:56:09.520 に答える