开发者

Loop that can take user input every time through

How can I ma开发者_开发技巧ke a loop that can take user input every time it loops?

#include <stdio.h>

#define WORD "jumble"
#define JUMBLED "mleujb"

int main()
{
    char string[6];
    int i = 0;
    printf("The jumbled word is ");
    printf(JUMBLED);
    printf("\nCan you guess the original: ");
    while(i == 0)
    {
        scanf("%d", string);
        if (string == "exit")
        {
            return;
        }
        if(string == WORD)
        {
            i++;
            printf("Kudos! You've guessed the word!");
        }
        else
        {
            printf("English please, good sir. Guess again.\n");
        }
    }
}

What I had hoped for was that every time the program went through the loop, it would want a new input with the scanf function. However, that apparently does not work that way. Instead, the program takes the value of the first scanf and uses it over and over again. If it is the wrong word, it will have an infinite loop.

This program has more than a few bugs in it: for instance, it does not actually compare the input to the actual word yet. As that does not pertain to the question, it is not my immediate concern.


you are using scanf() wrongly instead of scanf("%d",string) use scanf("%s",string) as %d is used for decimal input and %s is used for string input


Pseudo code for helping you precisely is not great Also can you define a bit better your question ? you don't really say what is going wrong

but here is my guess

your test is i ==0 which means as soon as your user inputs the right word your exiting your loop...

I would guess your looking for something like

exit_condition = 0;

while (exit_condition == 0)
{

   read keyboard entry

    if(condition to exit loop)
    {

        exit_condition = 1;
        printf("correct")
    }
    else
    {
        printf("try again")
    }

}

Concerning the tests I think you need to read up bit on input and tests

try this

http://www.arachnoid.com/cpptutor/student1.html


  1. scanf is incorrect for getting input string. It should be scanf("%s", string) as pointed out by others
  2. String comparison cannot be done by using == in 'C'. It will only compare the address of two strings which will fail. Use 'strncmp' function instead.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜