从c中的用户输入中读取2个整数
所以我的程序必须从用户输入中读取2个整数并打印出来。我使用scanf,程序将在输入错误时退出。但是,当输入为“3+2”或“3-2”时,scanf忽略“+”和“-”符号,并将3和2作为两个整数输入读取。我想要3+2和3-2作为坏输入,程序将退出。我怎样才能修好它从c中的用户输入中读取2个整数,c,input,integer,scanf,C,Input,Integer,Scanf,所以我的程序必须从用户输入中读取2个整数并打印出来。我使用scanf,程序将在输入错误时退出。但是,当输入为“3+2”或“3-2”时,scanf忽略“+”和“-”符号,并将3和2作为两个整数输入读取。我想要3+2和3-2作为坏输入,程序将退出。我怎样才能修好它 int num1, num2; if (scanf("%d%d", &num1, &num2) != 2) { //bad input, exit the program } else {
int num1, num2;
if (scanf("%d%d", &num1, &num2) != 2) {
//bad input, exit the program
}
else {
//print the two integers
您必须自己验证字符串。考虑以下事项:
#include <stdio.h>
#include <ctype.h>
#define MAX_STR_LEN (50)
int main(void)
{
char str[MAX_STR_LEN] = {0};
int num1, num2;
printf("Enter two numbers: ");
fgets(str, MAX_STR_LEN, stdin);
for(int i = 0; i < MAX_STR_LEN; i++)
{
if(!isdigit(str[i]) && (!isspace(str[i])) && (str[i] != '\0'))
{
if((i != 0) && (str[i - 1] != ' ') && ((str[i] != '+') || (str[i] != '-')))
{
printf("'%c' is bogus! I'm self destructing!", str[i]);
return -1;
}
}
if((str[i] == '\n') || (str[i] == '\0'))
break;
}
sscanf(str, "%d%d", &num1, &num2);
printf("You entered %d and %d. Good job. Pat yourself on the back.", num1, num2);
return 0;
}
#包括
#包括
#定义最大长度(50)
内部主(空)
{
char str[MAX_str_LEN]={0};
int num1,num2;
printf(“输入两个数字:”);
fgets(str、MAX_str_LEN、stdin);
对于(int i=0;i
逻辑如下:
您必须自己验证字符串。考虑以下事项:
#include <stdio.h>
#include <ctype.h>
#define MAX_STR_LEN (50)
int main(void)
{
char str[MAX_STR_LEN] = {0};
int num1, num2;
printf("Enter two numbers: ");
fgets(str, MAX_STR_LEN, stdin);
for(int i = 0; i < MAX_STR_LEN; i++)
{
if(!isdigit(str[i]) && (!isspace(str[i])) && (str[i] != '\0'))
{
if((i != 0) && (str[i - 1] != ' ') && ((str[i] != '+') || (str[i] != '-')))
{
printf("'%c' is bogus! I'm self destructing!", str[i]);
return -1;
}
}
if((str[i] == '\n') || (str[i] == '\0'))
break;
}
sscanf(str, "%d%d", &num1, &num2);
printf("You entered %d and %d. Good job. Pat yourself on the back.", num1, num2);
return 0;
}
#包括
#包括
#定义最大长度(50)
内部主(空)
{
char str[MAX_str_LEN]={0};
int num1,num2;
printf(“输入两个数字:”);
fgets(str、MAX_str_LEN、stdin);
对于(int i=0;i
逻辑如下:
由于要求似乎是输入行中两个数字之间的至少一个空格字符,因此代码需要类似于:
char s[2];
if (scanf("%d%1[ \t]%d", &num1, s, &num) != 3)
...format error...
此(%1[\t]
)在第一个数字之后立即查找空白或制表符。第二个数字前可能有额外的空格。除非您愿意,否则您不必对s
中的值执行任何操作
(针对精确的from进行了修复)由于要求似乎是输入行中两个数字之间至少有一个空格字符,因此代码需要类似于:
char s[2];
if (scanf("%d%1[ \t]%d", &num1, s, &num) != 3)
...format error...
此(%1[\t]
)在第一个数字之后立即查找空白或制表符。第二个数字前可能有额外的空格。除非您愿意,否则您不必对s
中的值执行任何操作
(为响应精确的from而修复)使用%n”
扫描以检测分离的空白
int num1, num2;
int n1, n2;
if (scanf("%d%n %n%d", &num1, &n1, &n2, &num2) != 2) {
; // bad input, exit the program
}
if (n1 == n2) {
; // no space between 1st and 2nd number, exit the program
}
另一种方法类似于@Jonathan Leffler,只接受至少1个空格
if (scanf("%d%*[ ]%d", &num1, &num2) != 2) {
; // bad input, exit the program
}
同意许多其他说法:使用fgets()/sscanf()
更好。使用%n”
扫描以检测分离的空白
int num1, num2;
int n1, n2;
if (scanf("%d%n %n%d", &num1, &n1, &n2, &num2) != 2) {
; // bad input, exit the program
}
if (n1 == n2) {
; // no space between 1st and 2nd number, exit the program
}
另一种方法类似于@Jonathan Leffler,只接受至少1个空格
if (scanf("%d%*[ ]%d", &num1, &num2) != 2) {
; // bad input, exit the program
}
同意许多其他说法:使用
fgets()/sscanf()
更好。从技术上讲,它是读取两个整数……当你给出两个值时,将它们分开。例32“-”符号不可忽略。 (还有“+”)您希望它做什么?当我运行这个程序并输入3-2时,它会打印3,然后是-2,这是两个整数。所以要求两个值之间有一个空格?有一个模因“对你接受的东西要慷慨,对你产生的东西要严格”(给予或接受),它说你应该尝试容忍次优的输入,但要小心产生正确形式的输出。你可能需要考虑它。从技术上讲,它是读取两个整数……当你给出这两个值分离它们。例32“-”符号不可忽略。 (还有“+”)您希望它做什么?当我运行这个程序并输入3-2时,它会打印3,然后是-2,这是两个整数。所以要求两个值之间有一个空格?有一个模因“对你接受的东西要慷慨,对你产生的东西要严格”(给予或接受),它说你应该尝试容忍次优的输入,但要小心产生正确形式的输出。你可能想考虑一下。第4点得到我的赞成票。第1点很关键-scanf
只是一个可怕的功能,应该扔进一口口袋里有石头的深井里。您可能需要考虑用户可以在两个值之间放置CRLF。我认为这是合法的,但您的代码(尚未)捕获它。任何时候scanf()
需要两个或多个数值,您可以在这些值之间有任意数量的换行符(从0开始)。这是fgets()
plussscanf()
与普通scanf()
相比的另一个优点。与字符串类似(%s
)。IIRC,只有两个转换说明符%[…]
,%c
和%n
不跳过前导空格;其他的都有,空白处包括换行符。第四点得到我的支持票。第1点很关键-scanf
只是一个可怕的功能,应该扔进一口口袋里有石头的深井里。您可能需要考虑用户可以在两个值之间放置CRLF。我想是的