My First Question

Started by
4 comments, last by Lanky007 17 years, 12 months ago
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?
Weco
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.
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. :)
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.
Weco
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B243444

While this is a VS 6.0 error, perhaps it applies to EE as well. Something to do with the stdlib, I personally couldn't decipher it but perhaps it'll help. Best of luck.
lol!
or wait, maybe this will do the trick:

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

its... deja-vu!

This topic is closed to new replies.

Advertisement