+ 4
My code works a bit wrong
I'm new in programming and I tried to do code, where you get random number and when it's more than 85 you will win and when it's less you will lose. What's wrong here? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main () { srand(time(0)); cout << 1 + (rand() % 100) << endl; int x = rand(); if ( x < 85) { cout <<"You lost!"<< endl; } if ( x >= 85) { cout <<"You won!"<< endl; } }
4 Respuestas
+ 10
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
srand(time(0));
int x=1 + (rand() % 100);
if ( x < 85) {
cout<<x <<endl;
cout <<"You lost!"<< endl;
}
if ( x >= 85) {
cout<<x <<endl;
cout <<"You won!"<< endl;
}
}
+ 6
You output a random number between 1 and 100, then you define a variable x which will be another random number, but not from the range between 1 and 100.
+ 2
you're missing "#i" in the beginning of your code
I'm not certain, but try using time(NULL) instead of time(0)
+ 1
thanks for all😘😁