hi, how would you get a certain part of code to run while the program is waiting for the input?

string in;
int n1 = 0;
n1++ // get this part of code to loop while the program is waiting for the input
cin >> in

thanks.

i dont think that would work. Are you talking about putting line 3 in another function? Cause if you are, that wouldnt work.

No, he means another thread. C++ currently doesn't come with a platform independent way to spawn threads, so you'll have to either rely on Operating System API calls, or use a library (I recommend Boost).

There are plenty of examples of both ways and I suggest reading some tutorials on multi-threading w/ C++ first.

Also, may I ask why you want to do this in the first place?

If it's for measuring the time the users takes to produce some input it's much easier to check the time before you ask for input, check it again right after input has been received and simply subtract the two.

Take a look at this if that is the case:
http://www.cplusplus.com/reference/clibrary/ctime/clock/
http://www.cplusplus.com/reference/clibrary/ctime/time/
http://www.cplusplus.com/reference/clibrary/ctime/difftime/

Edited 5 Years Ago by Insensus: n/a

thanks for all the help, but i need the timer to count in milliseconds. heres some code i pulled up

double time = .001;
time = time + .001;
Sleep(1)

if(!cin)
{
}
else
{
   cin >> n1;
}

Judging from the code you wrote I feel like you don't really know what you're doing.
So please make sure you understand my code instead of ignorantly copying it.

#include <iostream>
#include <time.h>

int main()
{
    time_t before = time(NULL);

    char input;
    std::cin >> input;

    //time_t after = time(NULL);
    //std::cout << difftime(after, before) << " seconds passed";
    std::cout << difftime(time(NULL), before) << " seconds passed";
}

Edited 5 Years Ago by Insensus: n/a

Yes, i understand the code and its exacly what i need but how would you change the seconds into milliseconds?

The ctime library only supports a granularity down to seconds. For milliseconds you must either use a non-portable library (such as GetTickCount()). The Boost Date_Time library offers sub-second resolution if the underlying platform does as well (which Windows does).

Standard solutions include dividing the result by CLOCKS_PER_SEC (be sure to cast either of the operands to double) and hoping for the best, or taking advantage of the new <chrono> stuff in C++0x. Both are non-portable at this time. The former is unconditionally non-portable and the latter depends on adoption of the upcoming standard in compilers.

ok, so are they telling you to put the GetTickCount() syntax in the time.h header file?

ok, so are they telling you to put the GetTickCount() syntax in the time.h header file?

Um...what?

ok listen, would at least ONE of you guys look at that link that Narue posted? if you guys know anything about what to do with the code, please share.

Good god, are you really that clueless? It's the same damn thing as Insensus' example, just with GetTickCount() instead of time():

#include <iostream>
#include <windows.h>
 
int main()
{
    DWORD before = GetTickCount();
 
    std::cin.get();
 
    std::cout << GetTickCount() - before << " milliseconds passed";
}
Comments
Getting there in the end
This question has already been answered. Start a new discussion instead.