number guesser

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

number guesser

Postby Spyder » Tue Dec 09, 2003 7:18 pm

I need some help on figuring how to do the math for guessing the users numbers. Here's what I came up with any comments will help!
Code: Select all
int high, low, guess, num;

cout << "Is the number 50? if it's to high enter 1, to low enter 2.\n";
cin >> guess;
while(guess)
{
num=(1+high)/2;
cout << "Is the number" <<num << " if it's to high enter 1, to low enter 2.\n";
}


what I don't get is how do I make the equation output the right answer. The high and low are not working i've tried it. anybody knows what i could do? plz let me know.
Spyder
 

Postby Justin » Wed Dec 10, 2003 2:05 am

ack. this has "lame" written all over it. let's see what we can do though with some pseudo code.

1. Tell the user to think of a number within some range
1a. Create variables HIGH and LOW. Assign them the respective
values of your guessing range (range 1-50. High = 50; low = 1)

2. A random number between LOW and HIGH
2a. If GUESS is too high
2a1. Assign the value of GUESS to HIGH
2b. If GUESS is too low
2b1. Assign the value of GUESS to LOW
2c. If GUESS is right, go to Step 4
3. Go to Step 2

4. Tell the user how über you are for guessing their number and ask them if the want you to guess another . . . or just exit.


Use rand() % (HIGH+1) + LOW to generate your random numbers.
User avatar
Justin
 
Posts: 158
Joined: Tue Sep 30, 2003 10:07 am
Location: CA

Txs! equation problem though

Postby Spyder » Thu Dec 11, 2003 6:13 pm

Thanks Justin!
I fixed my lame program lol ... I have one more problem though..... the equation doesn't output the right number.... do you know why its doing that?

Code: Select all
#include <iostream.h>
main()
{
cout << "This program will guess the users number from 1-100.\n";
int high,low,num,yes;


low=0;
high=100;

cout << "Guess of a number from 1-100.\n";

cout <<"Is the number 50? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
cin >> yes;
while (yes!=num)
{
  if(yes==1)
  yes=high;
  num=(low+high)/2;
  cout << "Is the number " << num << "? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
  cin >> yes;

  if(yes==2)
  yes=low;
  num=(low+high)/2;
  cout << "Is the number" << num << "? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
  cin >>yes;
}

cout <<  num << " is the number you guessed .\n";

return 0;
}
Spyder
 

Re: Txs! equation problem though

Postby RecursiveS » Thu Dec 11, 2003 6:20 pm

Spyder wrote:Thanks Justin!
I fixed my lame program lol ... I have one more problem though..... the equation doesn't output the right number.... do you know why its doing that?



Spyder: The answer you are going to get is "No".

People need more information - what is the number being output, as a result of what input......

Give that info and you might get a result :)
User avatar
RecursiveS
Site Admin
 
Posts: 1236
Joined: Thu Sep 18, 2003 8:33 am
Location: Dorset, UK

Postby spyder » Thu Dec 11, 2003 7:16 pm

lets say my number is 25 and i type in 1 for 50 being too high
so when it goes to the equation then the program guesses if my num is 50 again and well i dont know why its doing that
does any one know why?
Code: Select all
 
cout <<"Is the number 50? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
cin >> yes;
while (yes!=num)
{
  if(yes==1)
  yes=high;
  num=(low+high)/2;
  cout << "Is the number " << num << "? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
  cin >> yes;

  if(yes==2)
  yes=low;
  num=(low+high)/2;
  cout << "Is the number" << num << "? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
  cin >>yes;
}

spyder
 

Postby AlexL » Fri Dec 12, 2003 4:27 pm

Code: Select all
#include <iostream.h>

main()
{
   cout << "This program will guess the users number from 1-10.\n";
   int high,low,num,yes;

   low=0;
   high=100;

   cout << "Guess of a number from 1-10.\n";

   cout <<"Is the number 50? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
   cin >> yes;
   while (yes!=3)
   {
      if(yes==1)
      {
         high=num;
         num=(low+high)/2;
         cout << "Is the number " << num << "? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
         cin >> yes;
      }
      
      if(yes==2)
      {
         low=num;
         num=(low+high)/2;
         cout << "Is the number" << num << "? If it's to high enter 1, to low enter 2, correct number enter 3.\n";
         cin >>yes;
      }
   }

   cout <<  num << " is the number you guessed .\n";

   return 0;
}


you had 3 errors in it... just look at them yourself :)
AlexL
 
Posts: 52
Joined: Sun Nov 30, 2003 5:16 am

Postby AlexL » Fri Dec 12, 2003 4:38 pm

and the same in a bit cleaner code:

Code: Select all
#include <iostream.h>

main()
{
   cout << "This program will guess the users number from 1 till 100.\n";
   int low = 0, high = 100, num = 50, t;

   cout << "Guess of a number from 1 till 100.\n\n";

   do
   {
      cout <<"Is the number " << num << "?\nIf it's to high enter 1\nto low enter 2\ncorrect number enter 3.\n\n";
      cin >> t;

      if(t==1)
      {
         high = num;
         num=(low+num)/2;
      }
      else if(t==2)
      {
         low = num;
         num=(num+high)/2;
      }
   } while(t != 3);

   cout <<  "\n\n" << num << " is the number you guessed .\n";

   return 0;
}
AlexL
 
Posts: 52
Joined: Sun Nov 30, 2003 5:16 am

Postby spyder » Mon Dec 15, 2003 9:57 am

:D Thanks for the help alex!!! :D i figured it out!!
spyder
 


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests