Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

i have a very simple question(please answer)

Posted on 2003-03-01
5
Medium Priority
?
186 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:simpsons17371
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8050467
#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
 
LVL 6

Accepted Solution

by:
PlanetCpp earned 300 total points
ID: 8050482
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
 
LVL 1

Author Comment

by:simpsons17371
ID: 8050526
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
 
LVL 1

Author Comment

by:simpsons17371
ID: 8116554
does anyone actually know how to close a thread or is it just not possible
0
 
LVL 1

Author Comment

by:simpsons17371
ID: 8116569
blah blah blah
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

877 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