简体   繁体   中英

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. 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:

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?

Does anyone know why?

Your first code has two nested for loops, each with a local variable called 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.

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. Therefore, modifying i messes up the outer loop control.

If you want to use the while variant, declare a different variable, for example j and use that:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM