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
- scanf is incorrect for getting input string. It should be scanf("%s", string) as pointed out by others
- 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.
精彩评论