im trying to generate a random n umber but when i press enter once it stops working and put me back in console (im using repl.it)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int y = 1 ;
while (y < 5) {
int x = (1 + (rand() % 100));
if (cin.get() == '\n')
if (x == 2)
cout << "you got the legendary weapon";
else
cout << "Try again!";
return 0;
y += 1;
}
}
|
Last edited on
The cause is clearly that you didn't enclose return 0;
in your else block.
like this? because it still puts me back in console after one enter input
[code]
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int y = 1 ;
while (y < 5) {
int x = (1 + (rand() % 100));
if (cin.get() == '\n'){
if (x == 2){
cout << "you got the legendary weapon";
}
else {
cout << "Try again!";
return 0;
}
y += 1;
}
}
}
[cod}
Last edited on
Yes.
On a second thought, you should remove return 0;
entirely.
You do realize return 0;
exits the program, right?
Your original code was semantically incorrect, but I guess it's also kind of foolish to ask the user to "Try again" and then end the program.
Last edited on
thanks ill try it out and i didnt know that i thought it returned to the while
and it works thanks for the help lol
Last edited on