【发布时间】:2016-01-13 16:14:34
【问题描述】:
我尝试编写程序来扫描来自用户的字符串并检查用户输入的内容,如果它是真的,做一些事情,如果不是做其他事情。 我写的代码是这样的:
#include<stdio.h>
#include<conio.h>
int main()
{
char string[20];
printf("Enter a sentence : ");
scanf("%s",&string);
if(strcmp(string,"what's up")==0)
printf("\nNothing special.");
else
printf("\nYou didn't enter correct sentence.");
getch();
return 0;
}
但它不正确。我认为是因为程序在要扫描时无法识别空间。我该怎么办?(我是c新手,所以请解释一下你做了什么。)
【问题讨论】:
-
把
scanf("%s",&string);改成scanf("%19[^\n]", string);你也需要#include <string.h> -
scanf需要其他格式才能读取包含空格的输入。
标签: c string if-statement scanf strcmp