i am making a counting program, and i want to add a feature that quits the program, but i don’t really know how to do it. i have seen examples on how to do it, but all of them are for windows. please help
os is linux debian
i tried adding “i = getchar();” and then added an if: “if (i==1){break;}” but nothing happened. i was expecting it to get a value of 1 and then exit.
code:
#include <stdio.h>
//#include <threads.h>
#include <time.h>
#include <unistd.h>
int main() {
start:
struct timespec duration;
struct timespec remaining;
int time, num;
duration.tv_sec = 1;
duration.tv_nsec = 0;
time = 0;
num = 0;
while(time <= 32767)
{
int result = nanosleep(&duration, &remaining);
num++; //adds 1 to the number
if (result == 0)
{
printf("Seconds Passed: ");
printf("%dn", num); //prints the number
time++; //adds 1 to the time
}
else if (result == -1)
{
printf("Error with nanosleepn");
goto start;
}
}
getchar();
return 0;
}
New contributor
Yakov Kotsarenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.