Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a little help with a srand(time(o)) issue

Status
Not open for further replies.

Xoan

Programmer
Feb 16, 2007
4
US
Thanks for any help you can give on this, I have spent many hours looking at what I'm doing wrong. I have searched the web, read, books, and talked to a friend with more experience about and we all at a loss.

#include <cstdlib>
#include <ctime>
#include "stdafx.h"
#include <Iostream>

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

int x;


srand(time(0)); //The point of the problem


x = rand() % 100 +1; //Random number generated




if (x <= 49)
{
cout << "X is below the average." << endl;
}
if (x >= 50)
{
cout << "X is above the average." << endl;
}
else {

}







cin.get();
cin.get();

return 0;
}

I'm getting 1 error, sometimes after some adjustment the error changes so I put both of them below so maybe someone can explain them too me.

error C2064: term dose not evaluate to a function taking 1 argument

error C3861: 'time': identifier not found


Thanks for all the help you all can give.
 
Try this:
Code:
srand( (unsigned int)time( NULL ) );
 
> error C2064: term dose not evaluate to a function taking 1 argument
You get this if you have a local variable also called 'time'.

The position of your [tt]using namespace std;[/tt] is unusual.

Did the compiler create that [tt]stdafx.h[/tt] file when you created the project, or is it one you found on the net somewhere.

Please use the correct tags for posting code.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
#include <cstdlib>
#include <ctime>
#include "stdafx.h"
#include <Iostream>

int _tmain(int argc, _TCHAR* argv[])

{
using namespace std;

int x;


srand( (unsigned int)time( NULL ) ); //The point of the problem


x = rand() % 100 +1; //Random number generated




return 0;
}
I'm using Microsoft Visual Studio 2005, using the (win32 console application). Also I tried this out and it says this.
Error 1 error C3861: 'time': identifier not found



If I add in the (int time;) I get this error.
Error 1 error C2064: term does not evaluate to a function taking 1 arguments




Once again thank you for any help you can give n this matter
 
See if this compiles:
Code:
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{
    int x;

    time_t now;
    time( &now );
    srand( now );

    x = rand() % 100 + 1;    //Random number generated

    return 0;
}
 
If you are going to use pre-compiled headers, the #include "stdafx.h" needs to be the first #include in the file, otherwise the #includes above it are ignored. So your original code should work if you move the #include "stdafx.h" to the top.

You probably don't need precompiled headers, though. In the future, select Empty Project in the Application Settings tab when you create a new Win32 Console Application project. In this project you can probably turn off pre-compiled headers in the project properties, then remove the #include "stdafx.h".


Salem said:
The position of your using namespace std; is unusual.

If you are going to use a using directive, it is better to place it there than at the global level. Either way, it shouldn't cause a problem.
 
I tried the last posting and I got this to work...
time_t now;
time( &now );
srand( now );

when I cut/paste his exact code, however, when I try to work it into my program it gives me an error still (C2064)... so the issue must be a logical error I'm missing or something. I'm going to go over all my code and see if I can turn it up.

Thanks for the help so far.
 
Check out my post above. You need to move #include "stdafx.h" to the top (or remove it altogether as I described).
 
After a long search through my code I found the secondary issue which was giving me hell... and it was a simple over site from a past adjustment.


Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top