[英]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.