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; }
8 Antworten
+ 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
+ 2
because in the if/else blocks the line end == true is a comparison, not an assignment.
use
end = true;
instead.
+ 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....
+ 2
BTW, it is easier to spot errors when the code is correctly indented :-)
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;
}
0
Thanks man!
And i dont know what do you mean with "idented", can you explain me please?
0
i didnt know than that call idented. i used call "the thin than dont matter to the program" jejeje thanks for the information.
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.