【发布时间】:2014-05-14 14:00:49
【问题描述】:
对于家庭作业,我需要提示用户输入单词,直到他们输入 20 个单词或输入单词“完成”。现在我有一个带有 while 循环的函数,它扫描输入的每个单词,但是当用户键入“完成”时,循环并没有结束。
功能:
int wordentry(int row, int col, char a[20][100])
{
int i = 0;/* Counter */
printf("Enter 20 words you would like hidden in the puzzle, or type 'done' when finished:\n");
/* While the user hasn't entered 20 words, or the user hasn't entered the word 'done' the prompt to enter words keeps looping */
while(i < 20)
{
scanf("%c",a[i]);
if(strcmp(a[i],"done") == 0)
{
return(i);
}
i++;
}/* Ends while loop */
return(i);
}/* Ends function */
变量row 是用户确定的变量,它是每个单词要插入的单词搜索谜题的行长度,变量col 是用户确定的每列长度。数组a 是将每个单词存储为字符串的位置。返回变量i 用于跟踪用户输入的单词数。另外,除非有switch 语句,否则我不能使用任何break 语句。我很沮丧,因为我不知道如何正确地做到这一点。有人可以帮忙吗?
【问题讨论】:
-
建议
if (scanf("%99s",a[i]) != 1) Handle_Error_Somehow();
标签: c arrays loops multidimensional-array