question

A C++ built in random function generates random numbers in a given range. write a game program that asks a user to enter any value between 1 and 10 and the program should generate a value between 1 and 10 using a C++ random function and compare the value entered by a user to the value generated by the computer if the value generated by the computer if the values are equal the program should display "you have won" and "you lost" if they are not equal.


That's not a "question", it's a homework assignment. Post your attempt and explain where you're stuck...
at least start with something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int getUserNumber()
{
   int number;
   cin >> number;
   return number;
}

int getComNumber()
{
   return ( 1 + rand()% 10 );
}

int  main()
{
   int user = getUserNumber();
   int com = getComNumber();
   
   if(user == com )
  { 
   //to do
   }
else
{ 
   //to do 
 }
 return 0;
}
Last edited on
cnoeval haha funny...its not an assignment I came across that question in some book and I dint know were to start from and a the c++ function random generator,that gets random numbers.
Just look up the reference page. I searched "C++ Random Number" and found this: http://www.cplusplus.com/reference/cstdlib/rand/
and http://en.cppreference.com/w/c/numeric/random/rand

Also see this recent post: http://www.cplusplus.com/forum/beginner/174834/
Last edited on
okie I tryd to study that one posted by ericool but when compiring it says "rand" needs a prototype so comparing to the info on the link you posted am not sure how to hundle the "rand issue or sorting everything out
Topic archived. No new replies allowed.