Public Group

# two things

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! I'm new here and kinda new to pragramming. I've two questions. First of all, I want to show my first program I've just finished and kinda hope for some feedback on my code.
#include <cstdlib>
#include <iostream>
using namespace std;

bool stillRunning = true; // is program still running?
int randomNumber = 0;	  // this will contain the "random" number
int guess = 0;            // this will contain the user's guess
bool guessed = false;     // is the rand guessed?
char yesNo;               // for when the user has to make a y/n decision

// this will generate the "random" number... I want a real random number! :(
int GenNumber()
{
randomNumber = (rand()%25);
cout << "Ok, I have made up a number between 0 and 25" << endl;

return 0;
}

// getting the guess from the user...
int GuessNumber()
{
cout << "What's your guess?" << endl;
cin >> guess;

return 0;
}

// check if the user has guessed it or if he's close (or not)
int Check()
{
if ( guess == randomNumber ) {
cout << "Yay, you guessed the number! Congrats!" << endl;
guessed = true;
return 0;
}
if ( guess - randomNumber <= 5 && guess - randomNumber >= -5 ) {
cout << "OWW, you're close!" << endl;
}
else
cout << "Not even close, man." << endl;

return 0;
}

// ask if the user wants to play again (looky looky, it's fool proof ^^)
int PlayAgain()
{
bool correctInput = false; //there are always people who don't wanna listen

while ( correctInput != true ) {
cout << "Play again? y/n" << endl;
cin >> yesNo;

if ( yesNo == 'n' ) {
stillRunning = false; // the user wants to quit
correctInput = true; // but atleast he got the char right
cout << "Ok, bye bye!" << endl;
}
if ( yesNo == 'y' ) {
guessed = false; // we reset it for another round
correctInput = true;
cout << "Great!" << endl;
}
}

correctInput = false; // reset for next time

return 0;
}

//nice and simple loop
int main()
{
while ( stillRunning != false ) {
GenNumber();
while ( guessed != true ) {
GuessNumber();
Check();
}
PlayAgain();
}

return 0; // ok, we're done
}


I'm so proud ^^ Anyway, any sort of feedback on the current code is appreciated. The second is.... pointers. I'm currently reading C++ Primer, recommended in the "for beginners guide" and am just overwhelmed by it's explanation of pointers. What I do know is the basic reference/dereference stuff... I think :/
int thing = 0;
int *pthing;
pthing = &thing;
*pthing = *pthing + 1; // which makes thing = 1

Ok, that seems pretty straightforward. Is this all I need to know for the moment? Another thing that I just don't get is the actual use of it. For example, where in my number guess program could this be of use? Ok, that was it for now :D edit: hmm.. how do I make code-like text? [.code][./code] is not the answer, so it seems. edit2: ah got it. [Edited by - Deere on July 8, 2005 9:19:45 AM]

##### Share on other sites
Quote:
 Is this all I need to know for the moment?

That is pretty much the basics, yes.

Quote:
 Another thing that I just don't get is the actual use of it. For example, where in my number guess program could this be of use?

In a number guessing program it would not be of much use. One use is that you can use it to 'return' multiple values from a function. For example:
void f( int* pIntOne, int* pIntTwo ){  *pIntOne = 1;  *pIntTwo = 2;}int i, j;f( &i, &j );

The more useful use comes when dealing with things bigger than single integeres. Consider a structure of which you know little, only that it takes up a lot of memory:
struct SBig{ ...};

Suppose you want to do something with it in a separate function. Without pointers you would need to pass it to the function (i.e. copy it) and return it (i.e. copy it again). More efficient is pass-by-reference:
void f( SBig* pBig ){ ...};

There is much more to it, of course. Also, in C++ there are references which many coders find better in such cases.

Greetz,

Illco

##### Share on other sites
As for the code, I think it's very nicely written, and adequately commented. Very well done!

##### Share on other sites
Quote:
 Original post by IllcoThere is much more to it, of course. Also, in C++ there are references which many coders find better in such cases.

Yes. In C++ you would do: void f(SBig& big) { ... }, and modifying big within the function would directly modify the variable you passed to it.

Quote:
 First of all, I want to show my first program I've just finished and kinda hope for some feedback on my code.

Not bad at all for a first program. Now try to get rid of all those global variables. I notice you don't really make use of the return values of your functions (why return 0 ?) PlayAgain() could return true or false based on whether the player wants to play again. GuessNumber() could return the number picked by the player. And so on and so forth.

##### Share on other sites
wow, quick replies.
Ok, The need for pointer is clear now. Thanks for the explanation.
And I will try to modify the code a bit, according to your suggestions.
Thanks again :)

##### Share on other sites
Yes it's a very good program for a first one.As everybody said rid of the globals and use function's return value.Like

int GenNumber() //some func that returns int without parameters
{
int randomNumber = (rand()%25);
cout << "Ok, I have made up a number between 0 and 25" << endl;
return randomNumber;
}

int main()
{
int number //local variable that will hold the value of GenNumber

while ( stillRunning != false ) {
number = GenNumber();
.....

This will help you if you want to just copy-paste your GenNumber function in another program which right now you cant because in that another prog you must have global variable named randomNumber.This makes your prog more modular.

And for the pointers , yeah you got it right.I got a lot of problem understanding them until i realised they are like shortcut to some file.

Anyway good job!!!

##### Share on other sites
EDIT: Sorry page loading has stoped when i was replaying to topic so i send it again

##### Share on other sites
Ok, prog has changed a bit...

#include <cstdlib>#include <iostream>using namespace std;// this will generate the "random" number... I want a real random number! :(int GenNumber(){	int randomNumber;	randomNumber = (rand()%25);	cout << "Ok, I have made up a number between 0 and 25" << endl;	return randomNumber;}// getting the guess from the user...int GuessNumber(){	int userGuess;	cout << "What's your guess?" << endl;	cin >> userGuess;	return userGuess;}// check if the user has guessed it or if he's close (or not)bool Guessed(int* pnumber, int* pguess){	if ( *pguess == *pnumber ) {		cout << "Yay, you guessed the number! Congrats!" << endl;		return true;	}	if ( *pguess - *pnumber <= 5 && *pguess - *pnumber >= -5 ) {		cout << "OWW, you're close!" << endl;	}	else		cout << "Not even close, man." << endl;	return false;}// ask if the user wants to play again (looky looky, it's fool proof ^^)bool PlayAgain(){	bool correctInput = false; //there are always people who don't wanna listen	char yesNo;                // for when the user has to make a y/n decision		while ( correctInput != true ) {		cout << "Play again? y/n" << endl;		cin >> yesNo;			if ( yesNo == 'n' ) {			return false; // the user wants to quit			correctInput = true; // but atleast he got the char right			cout << "Ok, bye bye!" << endl;		}		if ( yesNo == 'y' ) {			correctInput = true;			cout << "Great!" << endl;		}	}		correctInput = false; // reset for next time	return true;}//nice and simple loopint main(){	bool stillRunning = true; // is program still running?	int number;               // container for the rand	int guess;                // this will contain the user's guess	bool guessed = false;     // is it guessed or not?	bool playAgain;           // if player wants to play again or not	while ( stillRunning != false ) {		guessed = false;           // this is just a reset		number = GenNumber();      // get our rand		while ( guessed != true ) {			guess = GuessNumber(); // get our guess			guessed = Guessed(&number, &guess); // compare and react accordingly		}	playAgain = PlayAgain();	if ( playAgain == false ) // when player wants to quit		stillRunning = false; // we tell the prog to quit	}	return 0; // ok, we're done}

What I noticed is that this forces you to take a different point of view. You actually have to keep track of the "dataflow" or whatever you call it.
It was a little harder than the first one, but it works.
Notice the use of a pointer ^^ This was for testing purposes, more or less, but still satisfying.
One problem I do have with this type of pragramming is that it can get a little messy (similar var names). I guess when I have some more experience it will solve itself.

##### Share on other sites
Very well done, I like it. The new design you implemented is very effective and keeps things nice looking. An even better enhancement to the design would be to implement the use of classes. Although it would seem kind of meaningless in this simple project (much like the pointer example), it would greatly improve the design of the program. If you make it through that step, you'll be well on your way [smile].

##### Share on other sites
Yes, that seems to be the next logical step, although I'm still not sure how to implement classes into a prog.
My first thoughts are that I make a class out of the vars in main and pass them to the functions with pointers. It seems like that would be kind of useless at the moment, but I realise that this is a very small program.
I'll just look into classes and see if I can enhance my prog even more.

Thanks for the kind words.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 25
• 9
• 57
• ### Forum Statistics

• Total Topics
632644
• Total Posts
3007625
• ### Who's Online (See full list)

There are no registered users currently online

×