i have a very simple question(please answer)

alright this is a very simple question to answer i guess, i just cant figure it out

i m new at C++, i have only been using it for a couple of months in school and am the best in my class and i cant figure this out, i use Visual C++

my question is (now i m asking for a program):

how can i write a program that tells the user ot think of a number between 1 and 100, then the computer guesses the number, and the user tells if his/her number is higher or lower, the computer continues to guess until the number is reached

it looks like a simple question compared to what i have been seeing so if anyone can help me it will be great

thanks
LVL 1
simpsons17371Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PlanetCppConnect With a Mentor Commented:
sorry my logic was messed up on the initial input, i wrote this in 5 minutes :o\ for more points i spend more time ;o)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <time.h>

int main(void)
{
     int number,guessed,numguessed,start,end = 100;
     number=guessed=numguessed=start=0;
     char correct;

     srand(time(NULL));

     do
     {
          cout<<"Enter a number from 1-100:";
          cin>>number;
          if(number<1 || number>100)
               cout<<"number out of range.\n";
     }while(number<1 || number>100);
 

     do
     {
          guessed = (rand()%(end-start))+1+start;
          cout<<"I guess "<<guessed;
          cout<<"\nAm i correct (h=higher,l=lower,c=correct):";
          cin>>correct;
          while(correct!='h' && correct!='H' && correct!='l' && correct!='L'
               && correct!='c' && correct!='C')
          {
               cout<<"invalid (h=higher,l=lower,c=correct):";
               cin>>correct;
          }
          ++numguessed;
          switch(correct)
          {
          case 'h':
          case 'H':
                    start = guessed+1;
               break;
          case 'l':
          case 'L':
                    end = guessed-1;
               break;
          case 'c':
          case 'C':
               if(guessed==number)
                    cout<<"i'm sooo psychic! and it only took "<<numguessed<<" guesses.\n";
               else
                    cout<<"ummm ok, actually it didn't.\n";
               break;
          };
     }while(correct!='c' && correct!='C');

    return 0;

}

0
 
PlanetCppCommented:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <time.h>

int main(void)
{
     int number,guessed,numguessed,start,end = 100;
     number=guessed=numguessed=start=0;
     char correct;

     srand(time(NULL));

     do
     {
          cout<<"Enter a number from 1-100:";
          cin>>number;
          if(number<1 || number>100)
               cout<<"number out of range.\n";
     }while(!(number>=1) && !(number<=100));
 

     do
     {
          guessed = (rand()%(end-start))+1+start;
          cout<<"I guess "<<guessed;
          cout<<"\nAm i correct (h=higher,l=lower,c=correct):";
          cin>>correct;
          while(correct!='h' && correct!='H' && correct!='l' && correct!='L'
               && correct!='c' && correct!='C')
          {
               cout<<"invalid (h=higher,l=lower,c=correct):";
               cin>>correct;
          }
          ++numguessed;
          switch(correct)
          {
          case 'h':
          case 'H':
                    start = guessed+1;
               break;
          case 'l':
          case 'L':
                    end = guessed-1;
               break;
          case 'c':
          case 'C':
               if(guessed==number)
                    cout<<"i'm sooo psychic! and it only took "<<numguessed<<" guesses.\n";
               else
                    cout<<"ummm ok, actually it didn't.\n";
               break;
          };
     }while(correct!='c' && correct!='C');

    return 0;

}

0
 
simpsons17371Author Commented:
thanks a lot i will give u all of my 75 points, sry dont have any more i just made this thing...your answer will help me in school and get me some extra credit so thanks again
0
 
simpsons17371Author Commented:
does anyone actually know how to close a thread or is it just not possible
0
 
simpsons17371Author Commented:
blah blah blah
0
All Courses

From novice to tech pro — start learning today.