Skipping over Scanf statement in C

后端 未结 4 1768
别那么骄傲
别那么骄傲 2021-01-29 15:50

I am writing an objective c program for hangman. I need to replicate another program which has been given to me. I have done most of it, but am having an issue. It has to replic

4条回答
  •  误落风尘
    2021-01-29 16:45

    The loop continue because you are never altering i and i = 0 is altways true; continue make that the code jump to the loop condicion, no end the loop. Also, as @AlterMann comments, you are not checking wordLength, i suggest this

        bool continueLoop = true;
        while (continueLoop ){
            printf("\n\n > Please enter a word length: ");
            scanf("%i", &wordLength);
            printf("\n\n");
    
            if (wordLength > 3 && wordLength < 14) {
                continueLoop  = false;
            }
            else {
                printf("number must be between 3 and 14 (inclusive)");
            }
        }
    

    Or maybe use break and end the loop without flags

        while (true){
            printf("\n\n > Please enter a word length: ");
            scanf("%i", &wordLength);
            printf("\n\n");
    
            if (wordLength > 3 && wordLength < 14) {
                break;
            }
            else {
                printf("number must be between 3 and 14 (inclusive)");
            }
        }
    

提交回复
热议问题