gpt4 book ai didi

c - 如何使用 scanf 获取包含空格的输入

转载 作者:行者123 更新时间:2023-11-30 17:36:36 26 4
gpt4 key购买 nike

我是 C 新手,这是我一直感到困惑的事情假设我有这样的代码我只想使用 char

    char a, b, c;
printf("input first character: ");
scanf(" %c", &a);
printf("input second character: ");
scanf(" %c", &b);
printf("input thrid character: ");
scanf(" %c", &c);

我多么希望也能在太空中阅读;我注意到这只能读取非空格字符,如果我想读取空格以及像这样的 c=' '; 该怎么办?我如何扫描这个空间;

现在通过听取使用 getchar() 的建议,我写了这个:

#include<stdio.h>

int main(void)
{
char a,b,c;
printf("input the first char:");
a=getchar();
printf("input the second char:");
b=getchar();
printf("input the third char:");
c=getchar();
return 0;


}

当我编译和运行程序时,怎么会发生奇怪的事情

程序输出是这样的

input the first char:
input the second char:input the third char:

现在它不再让我输入第二个字符,它最后直接跳到第三个请求,我只被要求输入 2 个输入,这很奇怪,因为程序在代码中明确要求输入 3 个。

现在这是我这样编写的程序,我将建议的内容添加到代码块中

int main(void)
{
int totalHeight=0, floorWidth=0, amountOfStories, amountWindowForTop, amountWindowForMiddle, amountWindowForBottom, windowHeight, middleWindowWidth, topWindowWidth, bottomWindowWidth, minimumHeight, minimumWidth;
int betweenDistanceTop, betweenDistanceMiddle, betweenDistanceBottom, edgeDistanceTop, edgeDistanceBottom, edgeDistanceMiddle;
char topFloorWindowContent, middleFloorWindowContent, bottomFloorWindowContent, windowBorder, floorBorder;
int tempMax, tempValue, tempSideDistance, tempBetweenDistance;
printf("please enter how many stories your building would like to have: ");
scanf("%d",&amountOfStories);
minimumHeight=amountOfStories*6+1;
while((totalHeight<minimumHeight)||((totalHeight%amountOfStories)!=1))
{
printf("please enter the totalHeight (minimum %d): ",minimumHeight);
scanf("%d",&totalHeight);
}
printf("please enter how many window building would have for top floor: ");
scanf("%d",&amountWindowForTop);
printf("please enter how many window building would have for middle floors: ");
scanf("%d",&amountWindowForMiddle);
printf("please enter how many window building would have for bottom floor: ");
scanf("%d",&amountWindowForBottom);
tempMax=amountWindowForTop;
if (tempMax<amountWindowForMiddle)
{
tempMax=amountWindowForMiddle;
}
if (tempMax<amountWindowForBottom)
{
tempMax=amountWindowForBottom;
}
while(floorWidth<tempMax)
{
printf("please enter the width of the building (Minimum %d): ",tempMax*4+1);
scanf("%d",&floorWidth);
}

char a, b, c;


printf("a:");
a=getchar();getchar();
printf("b:");
b=getchar();getchar();
printf("c:");
c=getchar();
printf("a=%c, b=%c, c=%c", a, b, c);
return 0;
}

现在有趣的部分是,如果我将这段代码放入大程序中,它不起作用,输出是这样的

please enter how many stories your building would like to have: 2
please enter the totalHeight (minimum 13): 2
please enter the totalHeight (minimum 13): 2
please enter the totalHeight (minimum 13): 13
please enter how many window building would have for top floor: 2
please enter how many window building would have for middle floors: 2
please enter how many window building would have for bottom floor: 2
please enter the width of the building (Minimum 9): 9
a:
b:*
c:a=
, b=
, c=

因为我们可以看到 a b c 全部读入\n 而不是空格 * 而 c 根本没有读到任何内容,这是为什么?

最佳答案

您的代码的问题是这样的:当您读取第一个字符(a)时,您按回车键(\n)插入下一个字符,所以现在在 stdin 上有一个您尚未阅读的 \n。当您尝试读取下一个字符 (b) 时,程序会从 stdin 读取前一个 \n,并且不允许您读取下一个字符。因此,当您使用 getchar() 读取字符,然后按键盘上的 Enter 键时,您需要第二个 getchar() 来删除 \n.这是可以解决您的问题的示例代码:

#include<stdio.h>

int main(void) {
char a, b, c;

printf("a:");
a=getchar();getchar();
printf("b:");
b=getchar();getchar();
printf("c:");
c=getchar();
printf("a=%c, b=%c, c=%c", a, b, c);
return 0;
}

对于您发布的编辑内容,您需要在获取 abc 的值之前放置所谓的“标准输入清洁器”:

while(getchar()!='\n');

它只是删除所有字符直到\n。请注意,当您发布的程序有大量来自键盘的输入时,有时您会遇到此问题,因为 stdin 中存在额外的字符。因此,这个问题的一般答案是尝试找出这些额外的字符(大多数情况下在某处有一个额外的 \n )可能在哪里,并使用像我提到的那样删除的函数,以便您可以继续从 stdin 读取。

关于c - 如何使用 scanf 获取包含空格的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22606755/

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