CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Question Age guessing game

    Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial guess). The user responds with 'O’ for older', ‘Y for 'younger' and ‘X’ for ‘you got it!’
    Use a while loop to ask user whether his/her age is ‘>’, ’<’ or ‘=’ the guess. Update the guess using the binary search method. Note that you are not allowed to use “break”. cant figure out where i'm going wrong. tried to change a few things when it wouldn't compile till i got lost in my own code.
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int binary_search(int A[], int key, int imin, int imax)
    {
        char A = Y || y || O || o || X || x;
    
        cout << "Are you 128 years old? Type O for older"                                    \
    
             << " Y for younger or X for 'you got it'" << endl;
        cin >> Y || y || O || o || X || x;
    
        if (A = Y || y)
    }
    /*****                                                                                    
    int binary_search (int A[], int key, int imin = 0, int imax = 128)                        
    {                                                                                         
        while (imin < imax)                                                                   
        {                                                                                     
            int imid = midpoint (i min, i max );                                              
            assert (i mid < imax);                                                            
            if (A[imid] < key)                                                                
                imin = imid + 1;                                                              
            else                                                                              
                imax = imid;                                                                  
        }                                                                                     
        if ((imax == imin) && (A[imin] == key))                                               
            return imin;                                                                      
        else                                                                                  
            return KEY_NOT_FOUND;                                                             
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Age guessing game

    What could these expressions mean:
    Quote Originally Posted by DawnC
    Code:
        char A = Y || y || O || o || X || x;
    
        ....
        if (A = Y || y)
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: Age guessing game

    The character A could be entered in as Y caps or y or O or o or X or x
    where O represents being older than the originak guess, y is younger and x means the programmes guess is correct.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Age guessing game

    Quote Originally Posted by DawnC View Post
    The character A could be entered in as Y caps or y or O or o or X or x
    where O represents being older than the originak guess, y is younger and x means the programmes guess is correct.
    1) C++ isn't the same as English-speak.

    2) To compare you use ==, not=.

    When specifying that a character can be equal to a set of characters, you have to use the correct boolean expression. How we say stuff in English doesn't equate to expressing the same thing in C++:
    Code:
    if ( A == 'Y' || A == 'y' || A == 'o' || A == 'O')
    That is how you specify if A is equal to a set of values in C++.

    Last, have you ever seen an expression as you wrote in your code in any C++ book or tutorial? If not, then there is a good reason you've never seen it. It's wrong.

    Regards,

    Paul McKenzie

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Age guessing game

    Quote Originally Posted by DawnC View Post
    The character A could be entered in as Y caps or y or O or o or X or x
    where O represents being older than the originak guess, y is younger and x means the programmes guess is correct.
    There seems to be a trend lately where people just try to guess at the syntax. What you posted isn't even close to correct, and honestly, pointing out the mistakes seems kind of pointless. Take your time with a good book and learn the basics. You won't get anywhere throwing stuff at the wall to see what sticks.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Age guessing game

    "There seems to be a trend lately where people just try to guess at the syntax"
    It's not just on codeguru forums either. Other forums are also having these types of questions posted.

    There's also seems to be a trend where people don't, or don't know how, to debug programs properly that compile but don't perform as expected.

    I'm going to be kind to the students and say the problem is that some tutors are setting some c++ programming problems without giving the students enough understanding in program design, testing/debugging and language syntax for some students to competently complete the designated assignment. Learning to program in a language like c++ from scratch can be terribly difficult at first until it all starts to fall into place. That's where having a good introductory book on c++ programming really helps. It takes practice and experience to 'think like a progammer'.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Age guessing game

    Quote Originally Posted by GCDEF View Post
    There seems to be a trend lately where people just try to guess at the syntax.
    And to add, there is no computer language that, as far as I know, can be learned from guesswork, regardless of how simple or easy the language may seem to be.

    Even a professional programmer using a tough language like C++ would need a tutorial, book, examples, on getting a BASIC program to work properly, if that person never saw BASIC before. I wish the tutors would emphasize that point about guessing -- learning programming is not a guessing game, but as you stated, it seems to be a trend now.

    Regards,

    Paul McKenzie

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Age guessing game

    Quote Originally Posted by Paul McKenzie View Post
    And to add, there is no computer language that, as far as I know, can be learned from guesswork, regardless of how simple or easy the language may seem to be.

    Even a professional programmer using a tough language like C++ would need a tutorial, book, examples, on getting a BASIC program to work properly, if that person never saw BASIC before. I wish the tutors would emphasize that point about guessing -- learning programming is not a guessing game, but as you stated, it seems to be a trend now.

    Regards,

    Paul McKenzie
    I was a COBOL programmer for years before switching to C++. Even then it took about three months before I was moderately productive at C++ and at least a year before I was comfortable with it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured