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 :/

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

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.

Share this post

Link to post

Share on other sites

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 this post

Link to post

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.

// 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 listenchar yesNo; // for when the user has to make a y/n decision

//nice and simple loopint main(){bool stillRunning = true; // is program still running?int number; // container for the randint guess; // this will contain the user's guessbool guessed = false; // is it guessed or not?bool playAgain; // if player wants to play again or not

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.

0

Share this post

Link to post

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].

0

Share this post

Link to post

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.