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

    Not sure what kind of code to write

    The program is called Hi-Low, where the computer generates a random number and the user inputs guesses.
    The problem im facing is trying to find out which loop or function to use to display the output properly.

    Everytime the user enters a guess, it displays if their guess is low or high and then tells them to try again until they get it correct.
    It also displays the number of tries at the bottom of the screen.

    What can I use to allow the attempts to scroll through, but then the bottom refreshes the number of tries.

    For example, after two attempts the screen would look like:
    --------------------------------------------------------------------
    Enter your guess <1 - 100>: 50
    LOW
    Enter your guess <1 - 100>: 75
    CORRECT

    Games played: 2
    --------------------------------------------------------------------

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

    Re: Not sure what kind of code to write

    You need to design the program first before you start coding! Break the requirements down into its parts. Think about what's needed in terms of user input, loops, output etc. From your previous assignments you should have all the knowledge needed to easily produce this program design and code. If you and I were sitting opposite each other to play this game, and I had to guess the number of which you thought, how would you do it?

    You can't keep asking us how to do your assignments. You have to apply knowledge learnt from previous assignments to new ones. This is how you learn how to program. If we keep providing you with the details of how you do things, you're not really learning - you're just trying things that we've said until it works without really understanding why.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2013
    Posts
    71

    Re: Not sure what kind of code to write

    So on this program, I have started off first by creating parts of the program individually.

    I created a intro page that just couts the rules, etc.
    created a random number generator and a loop for the user to guess numbers until they get it right..
    currently working on how to keep track of the number of tries, the number of games (number of times the program has been ran)
    and the average tries per game.

    I'll have to create loops for the game to start/stop
    A round loop for each round (don't understand what this means..waiting prof feedback)
    a loop for guesses (think I have this one done)

    so this time, im trying to design the program before I write it, so maybe ill create less errors.
    Im thinking a do while loop, but can I nest inside those? Surely so.

  4. #4
    Join Date
    Jan 2013
    Posts
    71

    Re: Not sure what kind of code to write

    well youre right, I have been. But like you said and someone said in my last thread.... Im going to try to write out the program steps on notebook and then write the code from that.

  5. #5
    Join Date
    Jan 2013
    Posts
    71

    Re: Not sure what kind of code to write

    So here's how Im thinking about the design.

    1. <Into>
    2. clear screen
    3. com generates num

    do (count number of tries, games & start/stop)
    while (guess != num)
    if (guess=low) {LOW, try again?}
    if (guess=high) {HIGH, try again?}
    else(guess=correct){CORRECT, start over?}

    Is that a decent design?

  6. #6
    Join Date
    Jan 2013
    Posts
    71

    Re: Not sure what kind of code to write

    well i received verification from the prof last night that i only display the stats after the user enters the correct answer. So, i did the code how I 'designed' and it worked...so woot.

  7. #7
    Join Date
    May 2007
    Posts
    811

    Re: Not sure what kind of code to write

    Congrats, feels good does it.

  8. #8
    Join Date
    Jan 2013
    Posts
    71

    Re: Not sure what kind of code to write

    yup, especially when i suck at 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