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]