#issue with delays

36 messages · Page 1 of 1 (latest)

gray notch
#

Hello, I'm using the chrono and thread libraries to create a delay. However, whenever I run the program, nothing happened. What could be my issue (code in thread)

covert vaultBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

gray notch
#
#include <iostream>
#include <random>
#include <chrono>
#include <thread>
#include <windows.h>
#include <mmsystem.h>
#include <string>
using namespace std;
using namespace std::chrono_literals;
using namespace std::chrono;
using namespace std::this_thread;

//setting up
char test1;
//date
int p = 1;
int dmy = 1;
int day = 1;
int month = 7;
int year = 1908;

void timerr()
{
    while( year > 0 )
    {
    sleep_for(seconds(p));
    ++day;
    if( day == 31 && month == 1 ) //jan
    {
        ++month;
        day = 0;
    }
    else if( day == 29 && month == 2 && year == 1908 || year == 1912 ) //feb, leapy
    {
        ++month;
        day = 0;
    }
    else if( day == 28 && month == 2 && year != 1908 || year != 1912 ) //feb, reg
    {
        ++month;
        day = 0;
    }
    else if( day == 31 && month == 3 ) //mar
    {
        ++month;
        day = 0;
    }
    else if( day == 30 && month == 4 ) //apr
    {
        ++month;
        day = 0;
    }
    else if( day == 31 && month == 5 ) //may
    {
        ++month;
        day = 0;
    }
    else if( day == 30 && month == 6 ) //jun
    {
        ++month;
        day = 0;
    }
    else if( day == 31 && month == 7 ) //jul
    {
        ++month;
        day = 0;
    }
    else if( day == 31 && month == 8 ) //aug
    {
        ++month;
        day = 0;
    }
    else if( day == 30 && month == 9 ) //sep
    {
        ++month;
        day = 0;
    }
    else if( day == 31 && month == 10 ) //oct
    {
        ++month;
        day = 0;
    }
    else if( day == 30 && month == 11 ) //nov
    {
        ++month;
        day = 0;
    }
    else //dec
    {
        month = 12;
        day = 0;
        ++year;
    }
    }
}

int main() 
{
    timerr();
    sleep_for(seconds(p));
    cout << "pause working";
    cin >> test1;
}```
thorn carbon
#

the issue is that for year to have a negative value (which only then will make it exit your loop), you'd have to wait literal years

sleek steppe
#

endless loop? ```c++
int year = 1908;

void timerr()
{
while( year > 0 )

thorn carbon
#

not exactly endless but for all practical purposes it might as well be

#

also that if cascade is not doing what you think it is

#

right now you're executing the single else block at the end every time you run around the loop

#

if it were working as intended however, the amount of time you'd have to wait before seeing any output would score in the hundreds of years lol

#

I just ran the numbers in fact

#

with your current faulty if cascade, which makes your program quicker than it really should, it would take an approximate 68 years before you see any output at all

#

that's the time it takes to increment year by one every second until it overflows to negative values

sleek steppe
#

numeric_limits<int>::max() + 1

thorn carbon
#

if your if cascade was working as intended, it would take about 365 seconds, give or take, before year is incremented by one

#

then we're looking at a mere 24855 years before you see any output

#

this much time ago we were still a species of cavemen and would still be for more than 15000 years onwards

thorn carbon
gray notch
#

while( year < 1915 )

#

I tried using this instead but it still doesn't work

thorn carbon
gray notch
#

yes

thorn carbon
#

have you saved the file and compiled it before you ran it again?

#

(stupid question but I'm very serious)

#

(you have no idea how many beginners forget to save their changes and then compile the file again)

gray notch
#

yeah I just did that

thorn carbon
#

and it still doesn't work? then you have to debug

#

what's your IDE

gray notch
#

should I use breakpoints?

#

visual studio

thorn carbon
gray notch
#

huh, weird, it runs if I comment out the timerr function

#

so there must be an error in that function

thorn carbon
gray notch
#

ok, fixed it!

#

ty all

#

!solved