Making a random number guessing game (exit failure? wtf)

This is a discussion on Making a random number guessing game (exit failure? wtf) within the C++ Programming forums, part of the General Programming Boards category; So it was the problem of the compiler not the language instruction I mean at runtime? Or does that still ...

  1. #16
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    So it was the problem of the compiler not the language instruction
    I mean at runtime? Or does that still count?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #17
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    Quote Originally Posted by rogster001 View Post
    I mean at runtime? Or does that still count?
    If code written properly (read: properly according to the stanrdard) was crashing just because of the goto instruction, it was problem of the compiler.
    I never put signature, but I decided to make an exception.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    goto statements are fine except they are evil. People are pressuring C++ standard boards ( IEEE 'n' stuff ) to remove it from the language as it is considered an illogical statement as it causes an 'illogical' execution flow. Simply put; if you need a goto statement, your code is in need of some TLC. Semantically goto statements can be viewed as sub-functions which breaks the semantics of a function which is a linear code flow.

    The truth is goto staments are used internally ( not goto staments exactly but jumps to instructions ) i.e. loops.

  4. #19
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    int rnd(int range);
    void seedrnd(void);
    
    int main()
    {
        int myguess, compguess, atry=101, gcount=0, rang=100, r=1, i;
        
        while (r ==1)
        {
              gcount=0;
              seedrnd();
              cout << "**************************************" << endl;
              cout << "Welcome to my number-guessing program!" << endl;
              cout << "**************************************" << endl;
              cout << "[press enter to continue] \n\n";
              getchar();
              cout << "I have picked a number between 1 and " << rang << endl;
              cout << "How many guesses do you think you'll need? \n\n";
              cin >> myguess;
              compguess=rnd(rang) + 1;
              cout << "\nOK, here we go... \n";
              while (atry != compguess)
              {
                    retry:
                    
                    gcount++;
                    cout << "Enter guess " << gcount << ": " << endl;
                    cin >> atry;
                    if (atry > compguess) {
                             cout << "Sorry, too high. Guess again. " << endl;
                             goto retry;
                             }
                    if (atry < compguess) {
                             cout << "Sorry, too low. Guess again. " << endl;
                             goto retry;
                             }
              cout << "FINALLY! \n\n";
              cout << "You needed " << gcount << " guesses, \n and estimated you would need ";
              cout << myguess;
              cout << ". Thanks for playing. " << endl;
              cout << "Press 1 to play the game again. " << endl;
              cin >> r;
              }
              cout << "\n";
              cout << i << " ";
              }
              return (0);
              }
              int rnd(int range)
              {
                  int r;
                  r=rand()%range;
                  return (r);
                  }
                  void seedrnd(void)
                  {
                       srand((unsigned)time(NULL));
                       }
    This one works (thanks to help from my programming class teacher and some tweaking from myself), but I was wondering if there was any way to implement a hint system that displays one of the digits (i.e. the tens or ones place).

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Before anything else,
    a) Get rid of goto.
    b) Indent properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #21
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    OK, but I tried it without goto and it loops when you guess.
    How do I work around this?
    [edit- indent like this?
    Code:
     return (0)
          }
              int rnd(int range)
                {
                  int r;
                  r=rand()%range;
                  return (r);
                }
                    void seedrnd(void)
                    {
                        srand((unsigned)time(NULL));
                    }
    ?]
    [edit #2- this is handy for indenting knowledge Wikipedia ftw]
    Last edited by muffinman8641; 03-03-2011 at 08:07 AM.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    You do it logically! Use loops such as while.
    Formulate the proper logic in your mind, ie what is the invariant for which we loop?
    That is, what conditions must we satisfy to keep asking?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #23
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Hint? I can't think of anything! Replacing
    Code:
     if (atry > compguess)
    with
    Code:
     while (atry> compguess)
    and removing the gotos did, as I predicted, loop it.
    (by the way I accidentally lied. I meant to say that
    Code:
     if (atry > compguess) {
                             cout << "Sorry, too high. Guess again. " << endl;
                             }
    makes it go to "Finally" as if the guess was wrong AND correct!

  9. #24
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    You got MS Visual Studio???

  10. #25
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    No, Dev C++. Imma get a new one though, because I've been informed it's out of date. This makes me sad because I'm used to it. :[

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Hint:
    What do we want to do?
    We want to ask the user to input a number until the user answers correct.
    So I ask again: what is our condition for looping? When do we want to keep repeating the question?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #27
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    We want to keep looping it until the user's answer is equal to compguess.
    While creates an uncontrollable loop.
    If takes us right to the end of the program.
    If/Else does the same as if.
    I think you might be referencing one I'm not familiar with. For? Not real good at those...

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    No, while is perfectly fine for these things.
    So basically you said it yourself:
    Loop until input guess == compguess.
    While loops take inverted conditions, since they loop until the condition is false.
    That is, we want to loop so long as our condition guess == compguess is not met.

    Two ways to do this:
    while (!guess == compguess))
    or inverting the logic within and removing the not condition
    while (guess != compguess)

    Now put it into place and watch the magic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #29
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    What does !guess do? I know != is not equal to, so something related to that?

    It's like reverse psychology! You're a wizard!
    /thanks

  15. #30
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Oh God, I feel like a moron.
    Code:
    while (atry != compguess)
                {
                     retry:                 
                     gcount++;
                     cout << "Enter guess " << gcount << ": " << endl;
                     cin >> atry;
                     if ((atry > compguess) || (atry < 1)) {
                     cout << "Please enter a guess between 1 and " << rang << ". " << endl;
                     goto retry;
                     }
                      while (atry == compguess) {
                               cout << "Sorry, too high. Guess again. " << endl;
                               goto retry;
                               }
                      while (atry == compguess) {
                               cout << "Sorry, too low. Guess again. " << endl;
                               goto retry;
                               }
                 cout << "FINALLY! \n\n";
    I have no idea what to do with this.
    What about the if ((atry > compguess) || (atry < 1)) { section?
    Sorry for being annoying, I'm just a little confused.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 03:28 PM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21