Return Of Otto's C++ Question Topic!

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
Heres the code

<!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
// Program by Aaron Friedley
// November 3, 2003
// Program takes randomly generats number 0-20
// Then asks user up to 5 times to guess the random number
// If user does not guess program tells user they lost

#include <iostream.h>
#include <time.h>
#include <stdlib.h>

int main (void)
{
 cout << "Program by Aaron Friedley MWF 10:00";

 // Prototypes
 void guess_input (int);

 // Declarations
 int random_num;
 
 random_num = srandom (time(NULL)) % 19 +1;
 cout << "\nI'm thinking of a number between 1 and 20, can"
      << "you guess what it is?";
 guess_input (random_num);
 
 return 0; // Terminate main
}

// *** guess_input function ***
// Prompt user for guess
void guess_input (int random_num)
{
 int countdown = 0;
 int guess;
 
 cout << "\nInput guess: ";
 cin >> guess;

 // Prototypes
 void check_guess (int, int, int);
 

 check_guess (countdown++, guess, random_num);
 return; // Terminate guess_input
}


// *** check_guess function ***
// Check guess against random_num and countdown
void check_guess (int countdown, int guess, int random_num)
{
 if (countdown == 5)
  {
  cout << "You took too many guesses! Play again!";
  return;
  }
   
    else if (guess > random_num)
    {
    cout << "Guess is too high, try again!\n";
    guess_input(random_num);
    }
     
     else if (guess < random_num)
      {
      cout << "Guess is too low, try again!\n";
      guess_input(random_num);
      }
     
       else if (guess == random_num)
        cout << "Congratulations you guessed correct!\n";
       
 return; // Terminate check_guess
}
<!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

What I can't get to work is the generator to do 1-20, and I can't get the countdown for the 5 guesses to work. Any advice?

Comments

  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    srand should generate a number between 0 and 1 so you should by multiplying it by 19 then adding 1 and remember to turn it into an int =3

    so it's more like... (int)((random * 19) + 1)
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    edited November 2003
    CheckGuess is calling GuessInput, which sets the count back to 0. Also, I'm not sure but doesn't srandom only seed the generator, not generate a random number?

    Further reviw of the code: You seem to be violating quite a few coding standards, like declairing prototypes inside functions... You're also having a function that calls a function that calls it back, while thats not nessesarily bad, the way your doing it is.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Did that and its still only getting 1.
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    <!--QuoteBegin--OttoDestruct+Nov 3 2003, 06:14 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 3 2003, 06:14 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Did that and its still only getting 1. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    If I'm right srandom doesn't return anything. Therefore both of those methods are just adding 1+0 giving you 1.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    edited November 2003
    yeah, I think skulk's right... ~scratches head~
    use random/rand instead of srandom/srand =/


    <b>edit:</b> I would've mentioned something about the method of recursion and prototypes declared in functions skulky but otto's programming teacher is a mad person and actually wants stuff like that half the time o.O
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    edited November 2003
    <!--QuoteBegin--Geminosity+Nov 3 2003, 06:21 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Geminosity @ Nov 3 2003, 06:21 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <b>edit:</b> I would've mentioned something about the method of recursion and prototypes declared in functions skulky but otto's programming teacher is a mad person and actually wants stuff like that half the time o.O <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    My god.... where are they getting programming teachers now? I thought the bad ones were relatively few untill I met my VB instructor (who insists on using hungarian warts on EVERY variable) and you siad that...
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--SkulkBait+Nov 3 2003, 06:32 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SkulkBait @ Nov 3 2003, 06:32 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--Geminosity+Nov 3 2003, 06:21 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Geminosity @ Nov 3 2003, 06:21 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <b>edit:</b> I would've mentioned something about the method of recursion and prototypes declared in functions skulky but otto's programming teacher is a mad person and actually wants stuff like that half the time o.O <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    My god.... where are they getting programming teachers now? I thought the bad ones were relatively few untill I met my VB instructor (who insists on using hungarian warts on EVERY variable) and you siad that... <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    Yep. I've had points taken off three assignments because I decided to put prototypes in the global section. Professor claims it makes for better programs if you put it in the function thats calling it. I don't see the point of cluttering everything up.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    Remember kids... "those who can, do. Those who can't, teach" <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin.gif' border='0' style='vertical-align:middle' alt='biggrin.gif'><!--endemo-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Got random to work.. now I'm just having problems with the countdown still <!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html/emoticons/nerd.gif' border='0' style='vertical-align:middle' alt='nerd.gif'><!--endemo-->
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    <!--QuoteBegin--OttoDestruct+Nov 3 2003, 07:06 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 3 2003, 07:06 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Got random to work.. now I'm just having problems with the countdown still <!--emo&::nerdy::--><img src='http://www.unknownworlds.com/forums/html/emoticons/nerd.gif' border='0' style='vertical-align:middle' alt='nerd.gif'><!--endemo--> <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    Did you fix the problem I mentioned? What is the new code?
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Eh? Which would that be? I got everything working now except the stupid countdown, and I'm completely stumped on that since I can't just do the ++ or -- inside the guess_input function.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    move 'int countdown = 0;' into main then pass it along the recursion chain along with random_number and it'll probably work =P
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Doesn't work gemmy
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    you'd better post up your current code so everyone can have a looky =3

    as for me... I go beddy byes though if skulk, crystal or the rest haven't gotten it covered I'll give it a poke when I get up in the morning <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Current code

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>

    int main (void)
    {
    cout << "Program by Aaron Friedley MWF 10:00";

    // Prototypes
    void guess_input (int, int);

    // Declarations
    int random_num;
    int countdown = 0;

    srandom (time(NULL));
    random_num = random() % ((20+1)-1) + 1;

    cout << "\nI'm thinking of a number between 1 and 20, can"
         << "you guess what it is?";
    guess_input (random_num, countdown);

    return 0; // Terminate main
    }

    // *** guess_input function ***
    // Prompt user for guess
    void guess_input (int random_num, int countdown)
    {
    int guess;

    cout << "\nInput guess: ";
    cin >> guess;

    // Prototypes
    void check_guess (int, int, int);

    check_guess (countdown++, guess, random_num);
    return; // Terminate guess_input
    }


    // *** check_guess function ***
    // Check guess against random_num and countdown
    void check_guess (int countdown, int guess, int random_num)
    {
     if (countdown == 5)
       {
       cout << "You've run out of guesses! Play again!";
       }
     
        else if (guess > random_num)
         {
         cout << "Guess is to high, try again!\n";
         guess_input(random_num, countdown);
         }
       
          else if (guess < random_num)
           {
           cout << "Guess is to low, try again!\n";
           guess_input(random_num, countdown);
           }
       
            else if (guess == random_num)
             cout << "Congratulations you guessed correct!\n";
             else return;

    return; // Terminate check_guess
    }
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited November 2003
    #include <iostream>

    int main (void)
    {
    cout << "/me slams head on table. I gots it working. All I had to do was put a countdown++ line in the check_guess function with the code I posted last, (and get rid of the ++ on guess_input just to clean up)";

    return 0;
    }
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    edited November 2003
    Man though, that is such a waste of recursion. Whatever happened to using it to solve the Towers of Hanoi? You know... where it makes sense...
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    This line is the culprit:

    check_guess (countdown++, guess, random_num);

    Can you guess why?
    I'm not saying, because that would be too easy. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    <!--QuoteBegin--CrystalSnake+Nov 3 2003, 07:59 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 3 2003, 07:59 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->This line is the culprit:

    check_guess (countdown++, guess, random_num);

    Can you guess why?
    I'm not saying, because that would be too easy. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo--><!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Yeah, as it turns out, you don't have to move the ++ to a whole 'nother function, you can simply modify this line to achieve the desired effect.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    I always thought the standard versions were rand() and srand(), not random() and srandom(). Silly nonstandard compilers <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif'><!--endemo-->

    Anyway, wd for getting it working <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html/emoticons/wink.gif' border='0' style='vertical-align:middle' alt='wink.gif'><!--endemo-->
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    OttoDestruct:
    Does this teacher of yours teach assembler programming, too?
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--CrystalSnake+Nov 3 2003, 09:16 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 3 2003, 09:16 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> OttoDestruct:
    Does this teacher of yours teach assembler programming, too? <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    Not that I know of. I'm transferring university next year so it really won't even matter. As for the standard of srand and random, teacher explained that both can be used (didnt say whether it was compiler only) and that we should use srandom and random as srand and rand tend to repeat numbers a lot. That was her explanation anyway.
  • CreepieCreepie Join Date: 2003-02-19 Member: 13734Members
    <!--QuoteBegin--OttoDestruct+Nov 3 2003, 10:36 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 3 2003, 10:36 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Not that I know of. I'm transferring university next year so it really won't even matter. As for the standard of srand and random, teacher explained that both can be used (didnt say whether it was compiler only) and that we should use srandom and random as srand and rand tend to repeat numbers a lot. That was her explanation anyway.<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Mebbe - depends whether portability is an issue or not.

    I've never seen any code, production or otherwise, with prototypes in functions. In terms of C, local functions (those that never see the light of day outside the file) either don't have prototypes at all, or have them once at the top of the file. If using them in multiple files, the general accepted wisdom is put "publish" them in a .h file. In bad cases, extern is used.

    The idea of publishing interfaces and abstraction just seems to an alien concept to some people.

    I'd put the names of formals in prototypes. Although the compiler needs to know function details before it's used, it's also useful for us monkeys too.

    Hungarian is full of fruity goodness.
  • GeminosityGeminosity :3 Join Date: 2003-09-08 Member: 20667Members
    so did you get it working otto? =D
Sign In or Register to comment.