输出画面停留一秒?
The output screen stays for a second?
当我 运行 以下程序时,如果我在退出 main 之前只调用 getchar()
一次,控制台 window 只会保持打开状态一秒钟。如果我添加对 getchar()
的第二个调用,那么它将使控制台 window 保持打开状态。这是什么原因?
#include <iostream>
using namespace std;
void selectionSort(int [], const int, bool (*)( int, int ));
bool ascending ( int, int );
bool descending ( int, int );
void swap(int * const, int * const);
int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 1, 22, 2 ,44 ,3 , 4, 6, 0, 7, 5 };
int order;
cout << "Enter 1 to sort in ascending order and 2 for descending " << endl;
cin >> order;
if ( order == 1 )
selectionSort( a, arraySize, ascending );
if ( order ==2 )
selectionSort( a, arraySize, descending );
for ( int i = 0; i < arraySize; i++ )
cout << a[i] << " ";
cout << endl;
getchar();
//getchar(); only if i use this version of getchar output screen remains
return 0;
}
bool ascending ( int x, int y )
{
return x < y;
}
bool descending ( int x, int y )
{
return x > y;
}
void swap(int * const x, int * const y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void selectionSort(int work[], const int size, bool (*compare)( int, int ))
{
for ( int i = 0; i < size - 1; i++ )
{
int smallestOrLargest = i;
for ( int index = i + 1; index < size; index++ )
{
if ( !(*compare)( work[ smallestOrLargest ], work[ index ] ) )
swap( &work[ smallestOrLargest ], &work[ index ] );
}
}
}
这是因为输入流中还有输入。第一次调用 getchar()
会看到这个输入,抓取它然后 return。当你添加第二个 getchar()
时,没有更多的输入,所以它会阻塞,直到你按下回车键。如果你想确保输入缓冲区中没有剩余任何东西,你可以使用:
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
这最多消耗流中的 streamsize::max
个字符,直到它到达换行符,然后只要它没有读取 streamsize::max
个字符就消耗换行符。这应该为 getchar()
.
留下一个空缓冲区
当我 运行 以下程序时,如果我在退出 main 之前只调用 getchar()
一次,控制台 window 只会保持打开状态一秒钟。如果我添加对 getchar()
的第二个调用,那么它将使控制台 window 保持打开状态。这是什么原因?
#include <iostream>
using namespace std;
void selectionSort(int [], const int, bool (*)( int, int ));
bool ascending ( int, int );
bool descending ( int, int );
void swap(int * const, int * const);
int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 1, 22, 2 ,44 ,3 , 4, 6, 0, 7, 5 };
int order;
cout << "Enter 1 to sort in ascending order and 2 for descending " << endl;
cin >> order;
if ( order == 1 )
selectionSort( a, arraySize, ascending );
if ( order ==2 )
selectionSort( a, arraySize, descending );
for ( int i = 0; i < arraySize; i++ )
cout << a[i] << " ";
cout << endl;
getchar();
//getchar(); only if i use this version of getchar output screen remains
return 0;
}
bool ascending ( int x, int y )
{
return x < y;
}
bool descending ( int x, int y )
{
return x > y;
}
void swap(int * const x, int * const y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void selectionSort(int work[], const int size, bool (*compare)( int, int ))
{
for ( int i = 0; i < size - 1; i++ )
{
int smallestOrLargest = i;
for ( int index = i + 1; index < size; index++ )
{
if ( !(*compare)( work[ smallestOrLargest ], work[ index ] ) )
swap( &work[ smallestOrLargest ], &work[ index ] );
}
}
}
这是因为输入流中还有输入。第一次调用 getchar()
会看到这个输入,抓取它然后 return。当你添加第二个 getchar()
时,没有更多的输入,所以它会阻塞,直到你按下回车键。如果你想确保输入缓冲区中没有剩余任何东西,你可以使用:
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
这最多消耗流中的 streamsize::max
个字符,直到它到达换行符,然后只要它没有读取 streamsize::max
个字符就消耗换行符。这应该为 getchar()
.