【发布时间】:2019-08-04 20:46:58
【问题描述】:
我正在尝试询问 K 个单词以将它们添加到矩阵中。 我有两个问题:
我试图使条件 strlen(string) 必须小于 n(matrix size) 的大小。但是当它进入do while循环时,它永远不会退出。
如何让 for 循环重复直到输入 k 个单词?
几天前我已经尝试过了,并且运行良好。直到我改变了一些东西,它变得一团糟。
/* Enter the matrix dimension */
int n;
do{
printf("\nEnter the matrix size");
scanf("%d", &n);
}while(2>n);
/* Ask for the amount of words the user will enter */
int k;
do{
printf("\nInsert how many words you will enter:");
scanf("%d", &k);
}while(k<0);
/* k Words loop */
int amountOfWords=0;
char string[20];
int i;
for(i=0; i<k; i++, amountOfWords++)
{
do {
printf("\nEnter the %d word:\n", amountOfWords+1);
scanf("%s", &string);
}while(strlen(string) > n);
}
【问题讨论】:
-
我看不出你的代码有什么问题。第一个循环读取
n大于或等于2,第二个循环读取k大于或等于0,for循环读取k长度小于或等于@987654328的单词@.