Jump to content
  • Advertisement
Sign in to follow this  
Weco

My First Question

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

If you intended to correct an error in the post then please contact us.

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.";
				round++;
			}
		
	if (mynum < randnum){
			cout << "Too low.";
				round++;
			}
		
	if (mynum == randnum){
			cout << "We have a winner!" << endl;
				cout << "It took you " << round;
				cout << " tries.";
				break;
			}
		}

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

http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B243444

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
lol!
or wait, maybe this will do the trick:

http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B243444

its... deja-vu!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!