gpt4 book ai didi

c - 我无法输入字符。我的程序终止

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:54 24 4
gpt4 key购买 nike

这是我的项目代码。这还没有完成,因为我被最后一个 scanf(在 printf("Enter option:") 之后)困住了。按两个键后我的程序终止。我也试过 char 和 %c 但没有用。希望你能帮助我。

void main()
{

char user[20], pass[500];
int i, a;
clrscr();
gotoxy(30,7); printf("ACCESS THE SYSTEM");
gotoxy(28,9); printf("Username: ");
gets(user);
gotoxy(28,11); printf("Password: ");
for(i = 0; i< 500; i++)
{
pass[i] = getch();
if(pass[i] == 13)
{
pass[i] = 0;
break;
}
printf("*");
}
gotoxy(30,15);printf("ACCESS GRANTED!");
gotoxy(24,20);printf("Please press any key to proceed: ");
scanf("%d", &a);
topics();
getch();
}

int topics(){

int opt;
clrscr();
gotoxy(25,5);printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
gotoxy(25,6);printf("º Computer Programming Topics: º");
gotoxy(25,7);printf("ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
gotoxy(13,9);printf("Choose one:");
gotoxy(13,11);printf("[A] Conditional Statements");
gotoxy(13,13);printf("[B] Looping Statements");
gotoxy(13,15);printf("[C] Functions");
gotoxy(13,17);printf("[D] Arrays");
gotoxy(13,19);printf("[E] Strings");
gotoxy(13,22);printf("Enter option:");
gotoxy(13,23);scanf("%d", &opt);
getch();
}

最佳答案

给定这段代码,在 main() 中

    gotoxy(24,20);printf("Please press any key to proceed: ");
scanf("%d", &a);
topics();
getch();

行:`scanf("%d", &a);要求输入以“返回”键结束,该键在输入流中放置一个换行符,此处未使用。

topics() 函数包含一个 getch(),它确实从输入流中获取“返回”键。

注意:“返回”键不是 topics() 函数的有效输入。

然后用户必须为 main() 中的最终 getch() 输入另一个击键。然后执行在 main() 的末尾运行,导致程序退出。

建议:将 main() 中的行:scanf("%d", &a); 替换为 getch()

发布的代码,在调用 topics()(获得一次击键)之后,再击一次键,程序退出。

如果您希望程序继续执行,那么在调用topics() 之后需要执行一些其他代码。

关于c - 我无法输入字符。我的程序终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com