My code works a bit wrong | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; } }

10th Sep 2018, 4:45 PM
ArturNiko
4 Answers
+ 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; } }
10th Sep 2018, 6:01 PM
MDJ_
MDJ_ - avatar
+ 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.
10th Sep 2018, 5:11 PM
Anna
Anna - avatar
+ 2
you're missing "#i" in the beginning of your code I'm not certain, but try using time(NULL) instead of time(0)
10th Sep 2018, 4:53 PM
Jens-Emil Sønderup Müller
Jens-Emil Sønderup Müller - avatar
+ 1
thanks for all😘😁
10th Sep 2018, 6:10 PM
ArturNiko