Public Group

# 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.

## Recommended Posts

The following code was working fine until I started adding other SDK's to Visual C++ EE. Here's the code:
#include &lt;iostream&gt;
#include &lt;math.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
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 &lt;&lt; "The goal of this game is to ";
cout &lt;&lt; "to determine the random number ";
cout &lt;&lt; "(from 1 to 100) ";
cout &lt;&lt; "in a few tries as possible.";
cout &lt;&lt; endl;
while(mynum != randnum){
cin &gt;&gt; mynum;

if (mynum &gt; randnum){
cout &lt;&lt; "Too high.";
round++;
}

if (mynum &lt; randnum){
cout &lt;&lt; "Too low.";
round++;
}

if (mynum == randnum){
cout &lt;&lt; "We have a winner!" &lt;&lt; endl;
cout &lt;&lt; "It took you " &lt;&lt; round;
cout &lt;&lt; " 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 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:

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 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 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 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!

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002141
×