无法退出无限循环
Can't exit an infinite loop
我的程序通过输入任意键启动,然后用户看到一个颜色变化的文本“欢迎来到我的
程序”。现在,用户应该按任意键继续,但他无法退出正在改变文本颜色的无限循环。让我向您展示代码以便更好地理解。
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Press any key to start...";
int stop=getchar();
while(stop){
for(i=10;i<=15;i++){
cout <<("\n\t\t\t\t\t Welcome to my program\n");
SetConsoleTextAttribute(color,i);
Sleep(100);
system("cls");
}
}
当您使用 getchar()
从用户那里获取输入时,它将在整数 stop
中存储用户输入的缓冲区的十进制值。
例如:如果用户输入'0'
,stop
变量将存储值48(字符'0'
的十进制值)。
你的 while 循环将保持 运行 而 stop
不等于 0,这永远不会因为所有字符的十进制值不为 0,唯一的字符是它的小数值为 0 是 NULL
.
要解决您的问题,您需要根据需要添加退出选项。
如果你创建一个循环如下:
int myInt = 1;
while (myInt)
{
std::cin >> myInt;
}
循环将继续,直到用户输入 0。这是因为条件语句使用 0 作为 "false",在这种情况下,任何其他值作为 "true"。
您实施的问题是您只让用户输入一次,即使用户输入“0”(字符格式),关联的整数值也是 48。
您可以添加:
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Press any key to start...";
int stop=getchar();
while(stop){
for(i=10;i<=15;i++){
cout <<("\n\t\t\t\t\t Welcome to my program\n");
SetConsoleTextAttribute(color,i);
Sleep(100);
system("cls");
}
std::cin >> stop;
}
现在循环将在用户按下任意键时开始,然后内部循环将 运行,然后用户将需要再次输入一个键。如果这次用户输入“0”,while() 循环将中断,程序将继续。
如果您试图保持一个循环 运行宁 而 搜索用户输入以打破循环,那么您正在尝试更复杂的任务。如果您对此感兴趣,请查看 std::thread 库。
据我所知,您的代码存在的问题是您仅在代码中初始化变量 "stop" 一次,而从未在循环中初始化。因此,该程序只会要求用户输入一次。一个简单的解决方法是在 for 循环结束时不断要求用户输入:std::cin>>stop;
。此外,就像其他人已经说过的那样,如果用户输入“0”,它会使用 ASCII 隐式地将 char 变量类型转换为整数变量。由于“0”对应于整数值 48,因此会将 48 而不是 0 保存到变量中。您可以使用以下代码解决此问题:
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Press any key to start...";
int stop=getchar()-'0';
while(stop){
for(i=10;i<=15;i++){
cout <<("\n\t\t\t\t\t Welcome to my program\n");
SetConsoleTextAttribute(color,i);
Sleep(100);
system("cls");
}
std::cin>>stop;
}
这将是适合您的解决方案(包括评论)
#include <iostream>
#include <Windows.h>
#include <thread>
int main()
{
HANDLE color = GetStdHandle( STD_OUTPUT_HANDLE );
std::cout << "Press any key to start...";
bool stop = false; // use a Boolean instead of int
// doesn't really matter what the input is, so use getchar().
// Note, this is really just "enter". You can modify if you expect user to
// hit multiple keys before hitting enter
getchar();
// This line here will start a new thread which will wait for the user to hit enter
std::thread getInput = std::thread( [&stop] { getchar(); stop = true; } );
// Loop stays the same (except I inverse the "stop" variable to make more sense)
while ( !stop ) {
for ( int i = 10; i <= 15; i++ ) {
std::cout << ( "\n\t\t\t\t\t Welcome to my program\n" );
SetConsoleTextAttribute( color, i );
Sleep( 100 );
system( "cls" );
}
}
}
我的程序通过输入任意键启动,然后用户看到一个颜色变化的文本“欢迎来到我的 程序”。现在,用户应该按任意键继续,但他无法退出正在改变文本颜色的无限循环。让我向您展示代码以便更好地理解。
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Press any key to start...";
int stop=getchar();
while(stop){
for(i=10;i<=15;i++){
cout <<("\n\t\t\t\t\t Welcome to my program\n");
SetConsoleTextAttribute(color,i);
Sleep(100);
system("cls");
}
}
当您使用 getchar()
从用户那里获取输入时,它将在整数 stop
中存储用户输入的缓冲区的十进制值。
例如:如果用户输入'0'
,stop
变量将存储值48(字符'0'
的十进制值)。
你的 while 循环将保持 运行 而 stop
不等于 0,这永远不会因为所有字符的十进制值不为 0,唯一的字符是它的小数值为 0 是 NULL
.
要解决您的问题,您需要根据需要添加退出选项。
如果你创建一个循环如下:
int myInt = 1;
while (myInt)
{
std::cin >> myInt;
}
循环将继续,直到用户输入 0。这是因为条件语句使用 0 作为 "false",在这种情况下,任何其他值作为 "true"。
您实施的问题是您只让用户输入一次,即使用户输入“0”(字符格式),关联的整数值也是 48。 您可以添加:
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Press any key to start...";
int stop=getchar();
while(stop){
for(i=10;i<=15;i++){
cout <<("\n\t\t\t\t\t Welcome to my program\n");
SetConsoleTextAttribute(color,i);
Sleep(100);
system("cls");
}
std::cin >> stop;
}
现在循环将在用户按下任意键时开始,然后内部循环将 运行,然后用户将需要再次输入一个键。如果这次用户输入“0”,while() 循环将中断,程序将继续。
如果您试图保持一个循环 运行宁 而 搜索用户输入以打破循环,那么您正在尝试更复杂的任务。如果您对此感兴趣,请查看 std::thread 库。
据我所知,您的代码存在的问题是您仅在代码中初始化变量 "stop" 一次,而从未在循环中初始化。因此,该程序只会要求用户输入一次。一个简单的解决方法是在 for 循环结束时不断要求用户输入:std::cin>>stop;
。此外,就像其他人已经说过的那样,如果用户输入“0”,它会使用 ASCII 隐式地将 char 变量类型转换为整数变量。由于“0”对应于整数值 48,因此会将 48 而不是 0 保存到变量中。您可以使用以下代码解决此问题:
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
cout<<"Press any key to start...";
int stop=getchar()-'0';
while(stop){
for(i=10;i<=15;i++){
cout <<("\n\t\t\t\t\t Welcome to my program\n");
SetConsoleTextAttribute(color,i);
Sleep(100);
system("cls");
}
std::cin>>stop;
}
这将是适合您的解决方案(包括评论)
#include <iostream>
#include <Windows.h>
#include <thread>
int main()
{
HANDLE color = GetStdHandle( STD_OUTPUT_HANDLE );
std::cout << "Press any key to start...";
bool stop = false; // use a Boolean instead of int
// doesn't really matter what the input is, so use getchar().
// Note, this is really just "enter". You can modify if you expect user to
// hit multiple keys before hitting enter
getchar();
// This line here will start a new thread which will wait for the user to hit enter
std::thread getInput = std::thread( [&stop] { getchar(); stop = true; } );
// Loop stays the same (except I inverse the "stop" variable to make more sense)
while ( !stop ) {
for ( int i = 10; i <= 15; i++ ) {
std::cout << ( "\n\t\t\t\t\t Welcome to my program\n" );
SetConsoleTextAttribute( color, i );
Sleep( 100 );
system( "cls" );
}
}
}