I am using the delay function in C and its causing infinite loops

179 Views Asked by At

This allwos me to use time.h to add a delay function

void delay(int  seconds){
        
        
    // Converting time into milli_seconds
        
    int millsec = 1000 * seconds;
 
    // Storing start time
        
    clock_t start_time = clock();
 
    // looping till required time is not achieved
    while (clock() < start_time + millsec)
        ;
}

but this line "while (clock() < start_time + millsec)" causes random loops for no reason sometimes. Usually happens when i use this global function.

int AskRound() {
    
    m=0;
    
    while (Runde!=1&&Runde!=2){
    
        if (m>0){   //Ab der ersten missinput wird folgendes Ausgegeben 
                    
            printf("\nEingabe nicht erkannt. Bitte entweder -1- oder -2- eingeben \n");
            
            delay(8000);
                    
        }
        
        printf( "\n\nWollen Sie nochmal Spielen?\n"
                        "für Ja -1- Eingeben und für Nein -2- Eingeben:  ");  
                
        scanf(  "%d",&Runde);   //Eingabe Neue Runde?
        
        delay(8000);
                
        m = 1.0;        //Missinput detection   
                    
    }   

}

At random times this global function keeps looping and when activaiting the slowmotion feature in my compiler it show that the line "while (clock() < start_time + millsec)" is looping.

Can someone explain?

Also since i added the delay function some culculations dont work properly. for exaple trying to multiply an int sometime outouts 0 for no vissible reason but idk if it has to do with this or at least i dont think.

Thanks

i tried using sleep function but my compiler doesnt include windows.h and i dont know how to add it to the library or if that is even possible. i tried unistd.h and the sleep function doesnt work either but my compyler findes the unistf.h library.

0

There are 0 best solutions below