Solved

Using random numbers

Posted on 1998-10-14
4
191 Views
Last Modified: 2010-05-19
We are learning to use the
srand(time(NULL))
rand()%11
function command things, and our teacher asked us to make a "game" so to speak where the user enters a number, then the computer trys to guess it, you have to use while loops also. I tried this. (number being the users entered number, compguess being the random number the computer picks)
int compguess = rand()%number;
while(compguess != number)
  {
   count = count + 1; //to keep the amount of guesses
   cout<<compguess<<" was wrong"<<endl;  
  }
 cout<<"The computer took "<<count<<" times"<<endl;
well that just gave me a big long scrolling list of one number being wrong, can someone please tell me how I could go about doing this a.s.a.p. sorry its so long.
0
Comment
Question by:truancy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Accepted Solution

by:
lodin earned 100 total points
ID: 1175179
You need to set compguess every time you iterate through the loop.
while(compguess != number)
     {
      count = count + 1; //to keep the amount of guesses
      cout<<compguess<<" was wrong"<<endl;  
      compguess = rand()%number;
     }

0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1175180
In a case like this it as always a good idea to STEP through your code and see what the code you wrote really does.

Take a pen and paper and write down the variables after each step (or only the ones that changed). If you did this you would see why you got the same value all the time and it would be easy to spot the problem.

After doing this a number of times on paper it gets easier to step through with the values in your head to check your code.
0
 

Expert Comment

by:mmachie
ID: 1175181
>while(compguess != number)
>     {
>      count = count + 1; //to keep the amount of guesses
>      cout<<compguess<<" was wrong"<<endl;    
>      compguess = rand()%number;
>     }

does this produce an infinite loop?  i mean, won't rand()%number result in a number between 0 and (number - 1) inclusive?
sorry about the daft question, but perhaps i don't understand the % operator.

0
 
LVL 5

Expert Comment

by:icd
ID: 1175182
mmachie.

Quite right, the original question seemed to expect a number in the range 0-10 (inclusive) so this should be:-

compguess = rand() % 11;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 73
Embarcadero C++ Builder XE2 TDateTime 8 91
trigs fail! I thought I knew how to do trignometry 3 94
cmake and message 1 23
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question