9 Replies - 770 Views - Last Post: 06 October 2012 - 03:33 AM Rate Topic: -----

#1 icee3x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 12

How should I create this while loop?

Posted 04 October 2012 - 10:39 AM

I already have a program that will generate random numbers and i already have a variable that generates random numbers between 1-100, which i named oracle. My program needs to ask the user for a number between 1-100, and then get a random number from the oracle and the loop needs to end once the oracle guesses the users number. After the oracle guesses the correct number, the program needs to print out the number of tries it took the oracle to guess my number. However, my program has to get a number from the oracle in a while loop and end when the oracle guesses my number and this is where i don't know what to do. I haven't learned much about while loops from this book but i want to finish this program.
This is what i have so far, i know lines 15 and 16 have to be in a while loop somehow along with more information. Mainly i dont know how to use the constructor in the while loop.

4 #include <iostream>
5 using namespace std;
6 #include "Random.h"
7 
8 int main()
9 {
10 int variable;
11 
12 cout << "What is your secret number between 1 and 100?" << endl;
13 cin >> variable;
14 
15 Random oracle(1,100);
16 cout << oracle.get() << endl;
17 
18 }
19 


Is This A Good Question/Topic? 0
  • +

Replies To: How should I create this while loop?

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • Joined: 19-March 11

Re: How should I create this while loop?

Posted 04 October 2012 - 10:44 AM

The syntax of the while loop is pretty simple:

while (CONDITION)
{
  STATEMENTS
}


So you need to come up with a boolean expression that means "I'm not done yet", and whose value will change in the statement block.

Does that help?
Was This Post Helpful? 0
  • +
  • -

#3 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 161
  • Joined: 22-November 08

Re: How should I create this while loop?

Posted 04 October 2012 - 10:45 AM

The best way I could see to do it is once the user enters a number have the program set the oracles number to a variable, then use a while loop to test if the oracles number is the same as the users number.

while(oracle != variable){
   // code to create another random number
}
//code to do if the numbers are equal


Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • Joined: 19-March 11

Re: How should I create this while loop?

Posted 04 October 2012 - 10:49 AM

That looks like a good plan, except you can't use the same name for the oracle object and an int variable.

So a little more like this:

// declare oracle_guess and initialize it to a starting value here

while(oracle_guess) != variable){
   // code to get() another random number from the oracle object 
   // and set oracle_guess to that value
}
//code to do if the numbers are equal

Was This Post Helpful? 0
  • +
  • -

#5 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: How should I create this while loop?

Posted 04 October 2012 - 05:27 PM

View Postjon.kiparsky, on 04 October 2012 - 10:49 AM, said:

That looks like a good plan, except you can't use the same name for the oracle object and an int variable.

So a little more like this:

// declare oracle_guess and initialize it to a starting value here

while(oracle_guess) != variable){
   // code to get() another random number from the oracle object 
   // and set oracle_guess to that value
}
//code to do if the numbers are equal

You appear to be missing a matching parentheses in there, just saying.
Was This Post Helpful? 0
  • +
  • -

#6 icee3x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 12

Re: How should I create this while loop?

Posted 04 October 2012 - 07:47 PM

Ok i made some revisions and now my program will get some random numbers and end even though it isnt guessing my number. My loop needs to stop when the oracle guesses my number and then print out the number of times it took for the oracle to guess my number. This is what i have now,i just have line 28 there to check and see if my program is guessing my number

 
  4 #include <iostream>
  5 using namespace std;
  6 #include "Random.h"
  7 
  8 int main()
  9 {
 10   int variable;
 11 
 12   cout << "What is your secret number between 1 and 100?" << endl;
 13   cin >> variable;
 14 
 15 Random oracle(1,100);
 16 int oracle_guess = 0;
 17 
 18  while(oracle_guess != variable)
 19  { cout << oracle.get() << endl;
 20    oracle_guess = oracle.get();
 21 
 22  }
 23 
 24 
 25 
 26 if(oracle_guess == variable)
 27 {
 28   cout << oracle.get() <<" I Just Guessed Your Number Correctly" << endl;
 29 
 30 
 31 }
 32 
 33 
 34 return 0;
 35 }




Was This Post Helpful? 0
  • +
  • -

#7 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: How should I create this while loop?

Posted 05 October 2012 - 01:07 PM

View Posticee3x, on 04 October 2012 - 07:47 PM, said:

 
 18  while(oracle_guess != variable)
 19  { cout << oracle.get() << endl;
 20    oracle_guess = oracle.get();
 21 
 22  }


You should do this instead of that:
while(oracle_guess != variable)
{
    oracle_guess = oracle.get();
    cout << oracle_guess << endl;
}


Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,203
  • Joined: 19-March 11

Re: How should I create this while loop?

Posted 05 October 2012 - 01:17 PM

View PostDuta, on 04 October 2012 - 07:27 PM, said:

View Postjon.kiparsky, on 04 October 2012 - 10:49 AM, said:

That looks like a good plan, except you can't use the same name for the oracle object and an int variable.

So a little more like this:

// declare oracle_guess and initialize it to a starting value here

while(oracle_guess) != variable){
   // code to get() another random number from the oracle object 
   // and set oracle_guess to that value
}
//code to do if the numbers are equal

You appear to be missing a matching parentheses in there, just saying.



You appear to be missing the point of presenting a skeleton and letting the guy work it out from there. If I wanted to write his code for him I'd charge for it.

Just saying.
Was This Post Helpful? 0
  • +
  • -

#9 icee3x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 12

Re: How should I create this while loop?

Posted 05 October 2012 - 04:11 PM

ok, i have the program running and it is guessing my number correctly. Just one last thing, i have no idea how to print out the amount of guesses the oracle took in order to figure out my number.
Was This Post Helpful? 0
  • +
  • -

#10 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: How should I create this while loop?

Posted 06 October 2012 - 03:33 AM

View Postjon.kiparsky, on 05 October 2012 - 01:17 PM, said:

View PostDuta, on 04 October 2012 - 07:27 PM, said:

View Postjon.kiparsky, on 04 October 2012 - 10:49 AM, said:

That looks like a good plan, except you can't use the same name for the oracle object and an int variable.

So a little more like this:

// declare oracle_guess and initialize it to a starting value here

while(oracle_guess) != variable){
   // code to get() another random number from the oracle object 
   // and set oracle_guess to that value
}
//code to do if the numbers are equal

You appear to be missing a matching parentheses in there, just saying.



You appear to be missing the point of presenting a skeleton and letting the guy work it out from there. If I wanted to write his code for him I'd charge for it.

Just saying.

Woah, relax.
A: You'd charge for taking 15 seconds to write some code?
B: If you're going to present a skeleton, don't present them syntax errors, unless you enjoy spawning more threads.

This post has been edited by Duta: 06 October 2012 - 03:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1