[英]Why does my program work woth a for but now with a while loop?
I´m very confused as I just spent quite some time on debugging my program and could eventually fit it by swapping my while loop for a for loop.我很困惑,因为我刚刚花了很多时间调试我的程序,最终可以通过将我的 while 循环换成 for 循环来适应它。 The following code works just fine and outputs the words I entered:
以下代码可以正常工作并输出我输入的单词:
int status = 1;
int *ptrStatus = &status;
int no_words;
int random;
char *selected;
printf("Enter the no of words you want to enter.\n");
scanf("%d", &no_words);
// Input the words
char *words[no_words];
char word[32];
int length;
char *pointerWord;
for (int i = 0; i<no_words; i++) {
printf("Enter word number %d: ", i);
scanf("%s", word);
length = strlen(word);
for ( int i = 0; i<length; i++) {
word[i] = tolower(word[i]);
}
pointerWord = (char*)malloc(length+1);
strcpy(pointerWord, word);
words[i] = pointerWord;
}
// Print word list
printf("You entered:\n");
for (int i = 0; i<no_words; i++) {
printf("%s\n", words[i]);
}
This code however (the same but with a while instead of a for loop), outputs random characters:然而,这段代码(相同但有一段时间而不是 for 循环)输出随机字符:
int status = 1;
int *ptrStatus = &status;
int no_words;
int random;
char *selected;
// Get number of words
printf("Enter the no of words you want to enter.\n");
scanf("%d", &no_words);
// Input the words
char *words[no_words];
char word[32];
int length;
char *pointerWord;
for (int i = 0; i<no_words; i++) {
printf("Enter word number %d: ", i);
scanf("%s", word);
length = strlen(word);
// Why doesn´t this work??
while(i < length) {
word[i] = tolower(word[i]);
printf("i = %d, length = %d and word[i] = %c\n", i, length, word[i]);
i++;
}
pointerWord = (char*)malloc(length+1);
strcpy(pointerWord, word);
words[i] = pointerWord;
}
// Print word list
printf("You entered:\n");
for (int i = 0; i<no_words; i++) {
printf("%s\n", words[i]);
}
When I entered "Hello" for example, the output was @?>???M???H??H?}?H?例如,当我输入“Hello”时,output 是 @?>???M???H??H?}?H?
Does anyone know why?有谁知道为什么?
Your first code has two nested for
loops, each with a local variable called i
.您的第一个代码有两个嵌套
for
循环,每个循环都有一个名为i
的局部变量。 The inner i
"shadows" the outer i
: In the innermost loop i
refers to int i
from the inner loop, in the outer loop, but outside the inner loop i
is int i
of the outer loop.内部
i
“阴影”外部i
:在最内部的循环中i
指的是来自内部循环的int i
,在外部循环中,但在内部循环之外i
是外部循环的int i
。
for (int i = 0; i < no_words; i++) { // def i1
// i == i1
for (int i = 0; i < length; i++) { // def i2
// i == i2
}
// i == i1
}
In your second code, You use i
for the inner loop, too, but there is no new variable: It is the same i
as in the outer loop.在您的第二个代码中,您也将
i
用于内部循环,但没有新变量:它与外部循环中的i
相同。 Therefore, modifying i
messes up the outer loop control.因此,修改
i
会扰乱外循环控制。
If you want to use the while
variant, declare a different variable, for example j
and use that:如果要使用
while
变体,请声明一个不同的变量,例如j
并使用它:
for (int i = 0; i < no_words; i++) {
int j = 0;
// read word
while(j < length) {
word[j] = tolower(word[j]);
j++;
}
// store word as word[i]
}
Or write a function that lowercases a string.或者编写一个小写字符串的 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.