Return Of Otto's C++ Question Topic!
OttoDestruct
Join Date: 2002-11-08 Member: 7790Members

in Off-Topic
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?
<!--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
so it's more like... (int)((random * 19) + 1)
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.
If I'm right srandom doesn't return anything. Therefore both of those methods are just adding 1+0 giving you 1.
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
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...
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.
Did you fix the problem I mentioned? What is the new code?
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-->
<!--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-->
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;
}
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-->
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.
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-->
Does this teacher of yours teach assembler programming, too?
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.
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.