• 13
• 27
• 9
• 9
• 20

# My very first program

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

## Recommended Posts

I just started learning C++, because I want to learn to write games. I am trying to write a program that attempts to guess a number from 0 - 100, to the best of kwoledge the program seems right. Can anyone help me telling what am I doing wrong, or at least pointing me to the right direction. this is the program ------------------------------------------------------------------------------- #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { cout << "Think of a number between 1 and 100, don't type just keep it in your mind.\n"; system ("pause"); int highNum = 100; // highNum is set to 100 to limited the range of numbers generated from 0 - 100, the variable changes if guessedNum is too high. int lowNum = 1; // lowNumb is set to 1 and it will change if guessedNum is too low, assigning the value of guessedNum to lownumb and adding lowNumb to guessedNumb in the next itiration. do //loops the program while the guessed number is incorrect. { do //loops the generation of a random number until that number is smaller than highNum { cout << "Now I am going to attempt to guess that number...\n" srand(time(0)); //seeding the random number with the current time int randomNum = rand(); int guessedNum = (randomNum % highNum) + lowNum; } while(guessedNum < highNum); cout << "Your number is " << guessedNum << ". Is that correct? y or n.\n"; char answer; cin >> answer; if (answer == 'y') // evaluate answer, if the answer is yes it leaves the loop. break; cout << "Was my guessed too high or too low? h or l.\n"; char rangeNum; cin >> rangeNum; if (rangeNum == 'h') // evaluates if the guessed number was too high or too low, depending on that it assign new values to highNum and lowNum to reduce the range or number that will be generated. int highNum = guessedNum; if (rangeNum == 'l') int lowNum = guessedNum; int tries++; } while(answer == 'n'); return 0; } -------------------------------------------------------------------- this is the compile errors -------------------------------------------------------------- Compiler: Default compiler Building Makefile: "C:\Dev-Cpp\book excersise\Makefile.win" Executing make... make.exe -f "C:\Dev-Cpp\book excersise\Makefile.win" all g++.exe -c test2.cpp -o test2.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" test2.cpp: In function int main()': test2.cpp:19: error: expected ;' before "srand" test2.cpp:22: error: guessedNum' undeclared (first use this function) test2.cpp:22: error: (Each undeclared identifier is reported only once for each function it appears in.) test2.cpp:41: error: expected primary-expression before "int" test2.cpp:41: error: expected ;' before "int" test2.cpp:42: error: `answer' undeclared (first use this function) make.exe: *** [test2.o] Error 1 Execution terminated -------------------------------------------- any help would be nice, thanks.

##### Share on other sites
First of all use the [ source ] tags for posting code (this way it will also be formatted correctly). I see this all the time, people spamming the forum with long code posts which get horrendous formatting. It really annoys me. Read the Forum FAQ!

What you should do is to actually read the error messages the compiler gives you. It even tells you the lines where the errors have been found. If it's because of a missing ; it will even tell you exactly that, so you only have to look at the line and add it.

For the other errors: You can't access a variable in a while condition which is declared inside the braces! So move the variable declaration out.

You can't write int tries++! Again, declare the variable outside the while loop and initialize it to 0 and then increment it inside the loop.

##### Share on other sites
For a beginner, I think Python is probably a better language choice, since it's easier to pick up, has much less pitfalls than C++ and can just as well be used for game development.

Anyway. I see you're using Dev-C++, an old and outdated IDE. Nowadays, much better alternatives exist, such as Microsofts Visual C++ Express, or Code::Blocks.

As for your code, most has been adressed by Trenki already. I've got a few additional points: you're seeding your random generator everytime the inner do...while loop is executed, but random generators only need to be seeded once, so you should do that before the loops. Second, the inner do...while loop is unnecessary. int guessedNum = (rand() % (highNum - lowNum)) + lowNum; should do the trick (I haven't tested it, but I'll leave that up to you). Good luck. :)

##### Share on other sites
Quote:
 do //loops the generation of a random number until that number is smaller than highNum{cout << "Now I am going to attempt to guess that number...\n"srand(time(0)); //seeding the random number with the current timeint randomNum = rand();int guessedNum = (randomNum % highNum) + lowNum;} while(guessedNum < highNum);

Your while condition is incorrect. You want while(guessedNum > highNum).

That said, the while() is unnecessary (as Captain P hinted), but not without changing your code.

guessedNum = (randomNum % highNum) + lowNum; // Gives you a number in the range (lowNum, lowNum + highNum - 1)

Armed with that knowledge, you could change it to:

guessedNum = (randomNum % (highNum - lowNum + 1)) + lowNum; // Gives you a number in the range (lowNum, highNum)

Cheers,
Matt

##### Share on other sites
Quote:
 Original post by Captain Pint guessedNum = (rand() % (highNum - lowNum)) + lowNum; should do the trick (I haven't tested it, but I'll leave that up to you).

I think the range is off by 1 -- should be

int guessedNum = (rand() % (highNum - lowNum + 1)) + lowNum;

Cheers,
Matt

##### Share on other sites
Also, you should only seed your random number generator (using "srand(time(0));") once, at the beginning of your program, not in a loop.

##### Share on other sites
Quote:
 Original post by Captain PAnyway. I see you're using Dev-C++, an old and outdated IDE. Nowadays, much better alternatives exist, such as Microsofts Visual C++ Express, or Code::Blocks.

Personally I'd suggest Eclipse. It just has so many features that help WRITE code, and for me that's more important than the ones that do all that complicated stuff dealing with I don't even know what. Eclipse just makes the writing itself easier.

Although, I must admit, it works a lot better for Java.

I don't have anything to say that no one's mentioned already, but I wanted to comment on your comments. You comment well, but it's sometimes redundant.

It's not hard to tell that this evaluates the answer. What might not be readily apparent is that if it's yes, it leaves the loop. Especially because it's unnecessary seeing as you leave the loop as long as answer doesn't equal 'n'.

Comments shouldn't say what the code says. They should say what the code doesn't. If I could read your code and fully and quickly understand it with no comments, then your code needs no comments.
"do //loops the program while the guessed number is incorrect." was a good comment.

"if (rangeNum == 'h') // evaluates if the guessed number was too high or too low, depending on that it assign new values to highNum and lowNum to reduce the range or number that will be generated.
int highNum = guessedNum;"
This was actually redundant because it's not hard to tell that if rangenum is 'h', it'll make highnum the guessed num.

What might be a better comment:
// If the rangenum is high, make the highNum rangenum to help limit the guessing range.

Also, this will cause a runtime error when you define the same variable twice at the second pass of the loop. All variables used in a loop, should be defined outside the loop to avoid such an error.

Hope this has helped.

##### Share on other sites
Quote:
 Original post by Splinter of ChaosAlso, this will cause a runtime error when you define the same variable twice at the second pass of the loop. All variables used in a loop, should be defined outside the loop to avoid such an error. Hope this has helped.

Incorrect. It is perfectly valid C++ to declare a variable inside a loop scope. No runtime error will occur. You will, of course, want to initialize the variable before you use it, just like in any other scope.

##### Share on other sites
And my compiler agrees.

Sorry bout that.