我的代码在下面进行了一些字符串操作。我的问题是,如果没有输入节奏和/或文本,我想退出程序。换句话说,如果我按下ENTER(换行),并且没有输入任何内容以便为scanf()读取,那么程序必须完成。如您所见,我设置了一种条件,即当节奏不在范围[2,6 ],程序结束。退出无限循环
int main()
{
char text[80]; // To storage the text string
int rhythm; // To storage the rhythm value
int k;
banner();
for(; ;){
scanf("%i %79[^\n]", &rhythm, text);
if(rhythm < 2 || rhythm > 6) break; // Termination of loop
printf("%s\n", text); // Printing original text
conversion(text, rhythm);
printf("%s\n\n", text); // Printing modified text
}
bye();
return 0;
}
或许检查['scanf()的'](http://en.cppreference.com/w/c/io/fscanf)(你应该这样做无论如何其结果是,你不能相信你的变量,除非它报告它实际上扫描了*他们的东西*)。 – WhozCraig