Ok. I have this simple "test" code in which i am trying to achieve a perfect ctrl-c capture process, so that i can transfer it to my main project. Now, The capturing of the ctrl-c works perfectly fine, but the things that come after that are not what i expected.
For instance, if the user clicked ctrl-c right at the beginning of the code, the code enters the ctrl-c condition from: Hi. Name please:
, then if the user pressed ctrl-c accidentally and wants to return to code, shouldn't it return back to asking the user's name? But it skips to asking Day number 2. Enter amount saved:
which is like two inputs away, from the name then to the first day then to the second day. How am i supposed to modify the capture function so that it returns back to where it left off, and not after that?
Code:
#include <stdio.h>
#include <stdlib.h> //For Functions such as exit(0) and system("pause")
#include <signal.h>
#define DAYS 7
void siginthandler(int sig_mun){
char H;
system("cls");
signal(SIGINT, siginthandler);
printf("[+] CRITICAL STOP [+]\nOuch! Did you just press ctrl-c?\nSo You want to exit?\nY to exit.\nN not to exit.\n\nYour input: ");
scanf(" %c", &H);
if (H == 'Y'){
system("cls");
printf("Exited");
exit(0);
} else {
fflush(stdout);
}
}
main(){
signal(SIGINT, siginthandler);
int num = 1;
float ave, amt, tot = 0, WC = 1, Wave, WD;
char nme[40], F;
printf("Hi. Name please: ");
scanf("%s", &nme);
printf("Dear %s, we will be calculating your daily average savings.\nType your saved amt per day sccordingly.\n\n", nme);
while (1){
while (num <= DAYS){
printf("Day number %d. Enter amount saved: ", num);
scanf("%f", &amt);
if (amt < 0){
printf(" Wrong! Please retype\n\n");
} else {
num = num + 1;
tot = tot + amt;
}
}
printf("Total amount saved till now: %f\n", tot);
ave = tot / DAYS;
printf("Average is %.2f per day.\n\n", ave);
printf("Wanna continue?\nInput (y/n): ");
scanf(" %c", &F);
if (F == 'n' && WC == 1){
WD = tot;
Wave = WD;
printf("You have Input %.0f week\'s data.\n", WC);
printf("You have saved %.2f per week in average.\n", Wave);
system("pause");
exit(0);
} else if (F == 'n' && WC > 1){
printf("You have input %.0f week\'s data.\n", WC);
WD = WD + tot;
Wave = WD / WC;
printf("You have saved %.2f per week in average.\n", Wave);
system("pause"); //Allows user to enter a key allowing the program exits
exit(0); //Exit code
} else if (F == 'y'){
num = 1;
WC = WC + 1;
WD = WD + tot;
tot = 0;
}
}
return 0;
}