Why my do-while doesnt end?/ Por que mi do-while no finaliza? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why my do-while doesnt end?/ Por que mi do-while no finaliza?

I made this game but the do-while doesnt end, what is wrong? Hice este juego pero el do-while no finaliza, qué está mal? #include<iostream> #include<cstdlib> #include<ctime> #include<conio.h> using namespace std; int main(){ int n1,resultado; bool end=false; srand(time(NULL)); cout<<"\t\t\tJUEGO DE LA SUERTE"<<endl; cout<<"\t\t\t------------------"<<endl; do{ cout<<"\n\nElige un numero del 1 al 10: ";cin>>n1; resultado = 1 + rand() % 10; if( n1 == resultado ){ cout<<"\n\nLe diste!!"<<endl; end == true; } else cout<<"\nIntenta otra vez"<<endl; end == false; } while(end == false); getch(); return 0; }

29th May 2018, 9:44 PM
Nahuel Alejandro Cáceres
Nahuel Alejandro Cáceres - avatar
8 Answers
+ 5
What ifl meant by indented is that your code is aligned proportionally, for example, indent (align deeper) after decision branch (if..else, switch etc.) and also after loop start (do...while, for, while etc.) I summarized @ifl corrections as follows: if( n1 == resultado ) { cout<<"\n\nLe diste!!"<<endl; end = true; // => end == true; } else // two instruction lines following // else, need to use curly brackets { cout<<"\nIntenta otra vez"<<endl; end = false; // => end == false; } Hth, cmiiw
29th May 2018, 10:58 PM
Ipang
+ 2
because in the if/else blocks the line end == true is a comparison, not an assignment. use end = true; instead.
29th May 2018, 10:02 PM
ifl
ifl - avatar
+ 2
The block delimiters { and } are missing after else. so the line end=false is not part of the else block, and is always executed. so the while condition stays true....
29th May 2018, 10:16 PM
ifl
ifl - avatar
+ 2
BTW, it is easier to spot errors when the code is correctly indented :-)
29th May 2018, 10:17 PM
ifl
ifl - avatar
0
doesnt work, keep doing the same thing. After seen your comment, i realized that dont made that mistake in the variable resultado jeje #include<iostream> #include<cstdlib> #include<ctime> #include<conio.h> using namespace std; int main(){ int n1,resultado; bool end=false; srand(time(NULL)); cout<<"\t\t\tJUEGO DE LA SUERTE"<<endl; cout<<"\t\t\t------------------"<<endl; do{ cout<<"\n\nElige un numero del 1 al 10: ";cin>>n1; resultado = 1 + rand() % 10; if( n1 == resultado ){ cout<<"\n\nLe diste!!"<<endl; end = true; } else cout<<"\nIntenta otra vez"<<endl; end = false; } while(end == false); getch(); return 0; }
29th May 2018, 10:05 PM
Nahuel Alejandro Cáceres
Nahuel Alejandro Cáceres - avatar
0
Thanks man! And i dont know what do you mean with "idented", can you explain me please?
29th May 2018, 10:28 PM
Nahuel Alejandro Cáceres
Nahuel Alejandro Cáceres - avatar
0
i didnt know than that call idented. i used call "the thin than dont matter to the program" jejeje thanks for the information.
30th May 2018, 12:06 AM
Nahuel Alejandro Cáceres
Nahuel Alejandro Cáceres - avatar
0
Nahuel Alejandro Cáceres That's "indented" (not "idented") indentation is about leaving extra space at the beginning of a line, comparing to other lines. In c++ and similar languages(java, JavaScript,...) indentation is only for humans, as the compiler/interpreter uses just the curly braces { } for block delimitation. In some other languages such as python, the indentation is meaningful syntaxically, and the program will fail if it is incorrect. Hope this helps :-) Happy coding.
30th May 2018, 5:07 AM
ifl
ifl - avatar