This is a short piece of code I am writing. It's for a homework assignment but totally nothing to do with the homework assignment. I am trying to add flare to the project by adding fake system lag in the interface. I want the system to lag between half a second and 3 seconds on each line. I used define so I didn't have to type the whole line in between each step, but for some reason it always comes up with the same random time for delay... Any ideas?
// a function instead of a yukky macro:
inlinevoid systemLag()
{
usleep( rand()%(3000000-500000) + 500000 );
}
int main()
{
// srand once and only once
srand( time(nullptr) );
//define variables
fstream cityFile;
string cityChoice;
string userName;
//menu
cout << "This program written by Srand... good luck!\n";
systemLag(); // <- call the function
cout << endl;
// systemSeed; // <- get rid of this... only srand once!
systemLag(); // <- call the function
cout << "Logging into mainframe.....\n";
@Disch, I appreciate the suggestion on macros. I have modified my code to use a function instead. I am surprised I hadn't thought of that! The point to the project is to use functions to call different options later in the program!
I am still having the same issue where the function always seem to pick the same random number, providing the same amount of emulated lag. I don't want that. Should I add the seed to the systemLag() function? I'll try that, but hopefully I am making sense...
Thanks for the responses!
@Chervil you make a great point sir! I might be trying to get a value outside of my system's max... I will check that out too.
Problem resolved! Chervil, you were totally on track! I was past my RAND_MAX!!! So I modified the code as follows, using Disch's suggestion of using a function. That makes it SOOOO much easier to troubleshoot!