[C language] how to make loop until true? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[C language] how to make loop until true?

Like.. I need to input monster type, it must be large, small, pets, or endemic. I want to make that if the type I input is other than the one of that four type, it will loop until the input is true.. Please, help me.. I put my code here : https://code.sololearn.com/cozPXDjjCVne/?ref=app

9th Apr 2021, 8:37 AM
Briana
Briana - avatar
9 Answers
+ 2
You chained the strcmp checks wrongly there I think. Try: while(!(strcmp(...) == 0 || strcmp(...) == 0 || ...)) { }
9th Apr 2021, 12:51 PM
Schindlabua
Schindlabua - avatar
+ 2
`!=` is not the correct way to compare strings in c, that's why your code doesn't work. Have a look at the `strcmp` function! if(strcmp(monster, "Large") == 0) { //the strings are equal! }
9th Apr 2021, 8:42 AM
Schindlabua
Schindlabua - avatar
+ 2
Your way to loop until true is correct just use the strcmp method instead of != to compare it inside the while loop
9th Apr 2021, 9:39 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 2
No, if the user inputs the correct value you expect then the loop will not execute.
9th Apr 2021, 9:47 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
Arun Ruban SJ ok, thank you. So, i don't need to use ++ ?
9th Apr 2021, 9:44 AM
Briana
Briana - avatar
+ 1
Arun Ruban SJ thanks :)
9th Apr 2021, 9:50 AM
Briana
Briana - avatar
+ 1
Schindlabua it's work. Thanks
9th Apr 2021, 12:59 PM
Briana
Briana - avatar
0
Schindlabua ok, thankyou. But, how to loop until true?
9th Apr 2021, 8:52 AM
Briana
Briana - avatar
0
Arun Ruban SJ can you help me again? I already change my code, but it still not run https://code.sololearn.com/cozPXDjjCVne/?ref=app
9th Apr 2021, 12:45 PM
Briana
Briana - avatar