Sign in to follow this  

My First Question

Recommended Posts

The following code was working fine until I started adding other SDK's to Visual C++ EE. Here's the code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int rand_0toN1(int n);

int main(){
srand(time(NULL));	// Set seed for random numbers.
int randnum = rand_0toN1(100);
int mynum;
int round;
cout << "The goal of this game is to ";
cout << "to determine the random number ";
cout << "(from 1 to 100) ";
cout << "in a few tries as possible.";
cout << endl;
while(mynum != randnum){
	cout << "Please enter your number: ";
	cin >> mynum;
	if (mynum > randnum){
			cout << "Too high.";
	if (mynum < randnum){
			cout << "Too low.";
	if (mynum == randnum){
			cout << "We have a winner!" << endl;
				cout << "It took you " << round;
				cout << " tries.";

return 0;

int rand_0toN1(int n) {
	return rand() % n;
Here's the error message: 1>Compiling... 1>randomnumbergame.cpp 1>C:\Program Files\Microsoft Visual Studio 8\VC\include\cstdlib(23) : error C2039: 'exit' : is not a member of '`global namespace'' 1>C:\Program Files\Microsoft Visual Studio 8\VC\include\cstdlib(23) : error C2873: 'exit' : symbol cannot be used in a using-declaration Does anyone have a clue why I'm having problems with the STDLIB?

Share this post

Link to post
Share on other sites
No clue what is causing that. In the code, there are the things that start with & that I guess the board recognizes as the apppropriate symbol rather than the actual symbol. Was the correct source lang="cpp" thing used? anyways, try this:

If that doesn't work, then make sure in the environment variables (control panel -> system -> Advanced -> environment variables) you have in the "path=..." the correct folder which has all the include files. If that doesn't work, try just getting rid of any unused includes, and also the "using namespace std;" and just doing std:: before everything.

Share this post

Link to post
Share on other sites
The names of the headers you want are now properly spelled <cmath>, <cstdlib> and <ctime> respectively. Although you shouldn't need cmath here.

You should also note:

- your random number function returns from 0 to 99 inclusive, not 1 to 100 inclusive like your output message indicates.

- At the beginning of the program, 'mynum' is uninitialized and could be anything - this means there's a small chance it equals 'randnum' right off the bat.

- You have big problems (infinite loop) if the input value is not a number. See here for help.

- When you check similar conditions, consider using an else-if structure. Here, for example:

if (mynum > randnum) {
// too high
} else if (mynum < randnum) {
// too low
} else {
// equal

You should be able to see that you now don't need to write the check for equality: if the code gets there, then mynum isn't bigger or smaller, so it must be equal. Sherlock Holmes style deduction, you know. :)

Share this post

Link to post
Share on other sites
I tried your suggestions Zahlman and still no luck. I cleaned up the code while I was at it. I'll probably just reinstall the IDE, because I know it was working before. Thanks for the help guys.

Share this post

Link to post
Share on other sites
or wait, maybe this will do the trick:

its... deja-vu!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this