This programm isn't working for my debugger/compiler and I need some assistance figuring out what the problem is... and please dont tell me not to use system, I already know about it.
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int a=0;
int b=0;
int c=time(0);
int d=c;
cout << "Enter how long that you want this timer to run\n ";
cin >> a;
system("cls");
a+d=b;
while(b!=d){
cout << time(0) << endl;
Sleep(1000);
system("cls");
}}
I am trying to make a stopwatch from scratch for my PC.
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int a=0;
int b=0;
int c=time(0);
int d=c;
cout << "Enter how long that you want this timer to run\n ";
cin >> a;
system("cls");
b=a+d;
//loop Never ends
while(b!=d){
cout << time(0) << endl;
Sleep(1000);
system("cls");
}
cout << "Timer complete!";
}
what is supposed to happen is as the time of the creation of the computer running the program goes up that number is the time so if i take that number and add to it it should count up to the new number and stop.
You have the system sleep in the while loop, but the variables of b and d never get changed. The while loop needs to re-evaluate the current time to see if the requested timer length has passed.
I'm not on a windows machine right now, so I commented out the windows sleep command. I've used some of the other features of <ctime> here. (See http://www.cplusplus.com/reference/ctime/ )
I'm trying to create a count down timer based off of a counter that counts up and I want to simplify it as much as possible. I am also trying to use the count up from when the computer was made. "time(0);" this only tells me how long it would take.
You can use the Sleep to count up, I took that out because as I mentioned I can't use the Windows functions on this computer. But your loop is never ending because you never check to see if you've reached the end of the timed period. You start at a certain time, and your finish time is going to be that start time plus however many seconds the user says they want the timer to run. You need to reevaluate the current time in the loop so it will end eventually.
Edit: time(0) gets the current time. I'm not sure what you mean by the time when the computer was made.
Get the current calendar time as a value of type time_t.
The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer.
The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types
They were done at different times and possibly different time zones. What is the current time on your computer now? Mine is 8:30pm eastern US time on Monday.