Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: ruler501 on February 21, 2011, 11:01:06 pm
-
I have been trying to make a math quiz to assist me in finding out my own skill compared to others and possibly to help me in my math tutorial style book. Here is my code to try and generate 5 random numbers from 1-5. It is displaying strange errors
/* rand example: guess the number */
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int randomgen(int range, int bottom);
int main()
{
for(int i=0; i<5;i++)
{
int num;
num = randomgen(5, 1);
cout << num << endl;
}
system("PAUSE");
}
int randomgen(int range, int bottom)
{
int iSecret;
srand(time(NULL));
iSecret = rand()%range+bottom;
return iSecret;
}
Errors
'Chapter1.exe': Loaded 'E:\Book\Software\Chapter1\Release\Chapter1.exe', Symbols loaded.
'Chapter1.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
'Chapter1.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file
'Chapter1.exe': Loaded 'C:\Program Files\Alwil Software\Avast5\snxhk.dll', Cannot find or open the PDB file
'Chapter1.exe': Loaded 'C:\WINDOWS\system32\msvcp100.dll', Symbols loaded.
'Chapter1.exe': Loaded 'C:\WINDOWS\system32\msvcr100.dll', Symbols loaded.
'Chapter1.exe': Loaded 'C:\WINDOWS\system32\shimeng.dll', Cannot find or open the PDB file
'Chapter1.exe': Unloaded 'C:\WINDOWS\system32\shimeng.dll'
The program '[2980] Chapter1.exe: Native' has exited with code 0 (0x0).
I don't know what is going on please help
-
What are you compiling this with?
-
Microsoft Visual C++ 2010 Express Edition
-
first, cstdlib and stdlib.h are the same thing. Pick one (I recommend stdlib.h).
Second, you aren't using stdio.h (I don't think), so get rid of that as well.
Third, the iostreams are buffered. This means that they accumulate data in a buffer, then send in all out at once to somewhere (the console in this case). This write is called a flush. To get the data to be flushed to the console, you need to change
cout << num;
to
cout << num << endl;
or
cout << num << flush;
-
I updated the code in the first post and it is still giving me the same errors
-
Is it producing the output?
-
I have it working now I added one line of code It shows the output but those errors still pop up
EDIT: I just found a problem it only shows one number as output all 5 times
-
The errors are actually not errors. It is the VC++ debugger saying it has or hasn't loaded a PDB file for the specific assembly library. (I won't go into more detail, just know it doesn't matter)
The same-number bug is because you set the random seed on every call.
You should set the seed up front (before the for loop), then not mess with it afterwards. Otherwise, it is resetting the seed each time.
-
thank you it seems to be working now. I'll post here if any strange things start happening.
-
You're very welcome! :)