[英]How to stop a program based on user's input in C
我刚刚开始学习C,并且在基于用户输入的内容停止程序方面遇到问题。
#include <stdio.h>
#include <stdbool.h>
int main()
{
int a;
int b;
char c[5];
printf("Enter the two values you like to compare, type stop to end.\n");
while (c != "stop")
{
scanf_s(" %d %d %s", &a, &b, &c);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}
printf("Thanks for playing.");
getchar();
return 0;
}
我遇到的问题是必须在我的scanf_s中放入另一个变量c。 我该怎么做,以使用户不必在两个数字后面输入另一个单词? 另外,如何检查用户是否仅输入“停止”以停止程序? 顺便说一句,当我执行“ 10 10 stop”时,我现在拥有的方法也不会停止程序。 谢谢。
完整的工作代码:
#include <stdio.h>
#include <string.h>
int main()
{
int a;
int b;
char c[5] = {'\0'};
do {
printf("Enter the two values you like to compare, type stop to end.\n");
scanf("%d%d%s", &a, &b, c);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}
while (strcmp(c,"stop"));
printf("Thanks for playing.");
getchar();
return 0;
}
while (c != "stop")
您不能像这样在C语言中比较strings
,请使用string.h
可用的memcmp()
或strncmp()
库函数。 阅读有关它们的信息,以了解如何将它们实现为while
循环中的条件。
另外,要获取字符串输入,请使用
scanf_s(" %d %d %s", &a, &b, c); // Remove that litle '&' before 'c'.
注意:函数scanf_s
返回正确扫描的输入数,在继续输入值之前,您应该检查一下。
为了使用户在不显式输入“停止”的情况下停止,有很多方法:
1)使用do-while
循环并不断询问用户是否想玩更多游戏。
2)使用负数输入(例如-1
)退出。
使用下面的行以确保您的'c'扫描输入的字符串。
scanf(“%d%d%s”,&a,&b,c);
编辑:考虑将您的while替换为下面的行,以确保您停止工作。 包括“ string.h”
while (strcmp(c,"stop"))
这是固定版本...为了方便理解,我在代码中添加了注释...
#include <stdio.h>
#include <string.h>
int main()
{
int a;
int b;
char c[5] = {'\0'};
printf("Enter the two values you like to compare, type stop to end.\n");
while (strcmp(c,"stop"))
{
scanf("%d%d%s", &a, &b, c);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}
printf("Thanks for playing.");
getchar();
return 0;
}
这是因为您使用的是&c
而不是scanf_s
c
。 替换它,它应该工作。 另外, c
是一个string
,因此,您必须使用strcmp
而不是!=
。
编写此代码的一种更简单的方法是:
int main()
{
int a;
int b;
char c;
do
{
printf("Would you like to play?\nPress 'Y' for 'Yes' or 'N' for 'No'\n");
scanf( "%c", &c ) ;
/*scanf_s( "%c", &c, 1 ) ; */
if( c != 'Y' && c != 'y' )
break ;
printf("Enter the two values you like to compare\n" ) ;
scanf(" %d %d", &a, &b);
if (!(a^b))
{
printf("both are equal\n");
getchar();
}
else
{
printf("both are not equal\n");
getchar();
}
}while(1) ;
printf("Thanks for playing.");
getchar();
return 0;
}
首先,让我们更正您的程序: &c
(scanf_s的第三个参数)不正确。 它应该是c
(因为它已经是一个指针)。 根据文档, scanf_s
要求为所有%s
格式字符串指定大小 ; 因此,按照现在的操作方式,您应该已经编写了scanf_s(" %d %d %4s", &a, &b, c);
如果您将逻辑更改为“输入空白行以退出”,则该程序将更易于使用。 您可以查看scanf_s
的返回值来进行测试,该返回值将是从输入中正确读取的格式字符串的数量。
如果您需要输入字符串,则允许其中一个,然后查看用户写的内容以查看它是数字还是字符串:
#include <stdio.h>
#include <string.h>
int main() {
int a;
int b;
char c[32];
printf("Enter the two values to compare, or type stop to end.\n");
while (fgets(c, 31, stdin) != NULL && strncmp("stop\n", c)) != 0) {
// user did not request to exit and wrote something; maybe 2 numbers
if (sscanf(c, "%d %d", &a, &b) != 2) {
// but he did not write two numbers. Complain and repeat.
printf("please write two numbers to compare, or type stop to end.\n");
continue;
}
if (a == b) {
printf("both are equal\n");
} else {
printf("both are not equal\n");
}
}
printf("Thanks for playing.\n");
return 0;
}
fgets
读取整行,然后您可以尝试使用sscanf
解析它们。 与普通scanf
相比,它的优势在于您可以尝试根据自己在其中找到的内容以不同的方式解析同一行。 一般情况下,你不想来填补你的代码getchar()
如果要调试,调试器将为您停止。 如果您不进行调试,则需要通过输入重定向来测试或使用程序,而根本不需要getchar()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.