【发布时间】:2019-08-07 10:57:42
【问题描述】:
我正在通过 do while 语句使用 Mr 或 ms 或 Mrs 在姓名前输入姓名进行验证。我应该在while部分填写什么?。
是使用 strcmp 还是别的什么?
编码示例
do{
printf("Input Customer's Name [must have Mr. or Ms. or Mrs");
scanf("%[^\n]", &customerName);
}while(what should i fill here?);
【问题讨论】:
-
在 C 规范中明确提到使用仅输入流(如
stdin)调用fflush为 未定义行为。一些库将其添加为 non-portable 扩展。我建议您改为阅读整行,例如fgets然后使用sscanf解析字符串。并且记得检查sscanf返回的内容。哦,还有一些吹毛求疵:do ... while是 statement 而不是函数。
标签: c algorithm fgets c-strings memcmp