write this c program REQUIREMENTS Here are the requirements for error logging: -
ID: 3877968 • Letter: W
Question
write this c program
REQUIREMENTS
Here are the requirements for error logging:
- A function called LogError must write an error message to a text file, to the debug console, or to the
program console. This message must contain the following information:
o Date and time of occurrence
o Error message (passed to the function as a parameter)
- Use #define statements to identify:
o The output mechanism for the errors (text file, debug console or program console)
o The file path for the text file for the error log
- Use conditional compile statements in the LogError function to determine where the error message
output should go
Write a test program that demonstrates the use of the LogError function. Initially, write a program that does the
following WITHOUT the error logging:
- Ask for the user’s age
- If the input is non-numeric, you should give the user a friendly message stating that the input is nonnumeric
- If the input is null (empty string), you should give the user a friendly message stating that the input cannot
be blank
- If the input is less than 1 or greater than 110, you should give the user a friendly message stating that the
input is out of range
- If the age is valid (positive integer), simply say “Thank You” and end the program
- If there is any error in the age, the age should be asked for again
Once the program works, add error logging function calls in appropriate places to record where users make errors.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include <ctype.h>
#include<string.h>
#include <time.h>
#include<stdlib.h>
//Constants that represent the error destination and file path
#define TEXT_FILE "text_file"
#define DEBUG_CONSOLE "debug_console"
#define OUTPUT_CONSOLE "output_console"
#define FILE_PATH "log.txt"
//Global variable to represent error type
char *ERROR_TYPE;
//Function to check whether given input is numeric or not
int isNumeric(const char *s)
{
while (*s) {
if (isdigit(*s++) == 0)
return 0;
}
return 1;
}
//Function to log the error based on specified destination
void logError(char *ERROR_MESSAGE) {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char s[100];
strftime(s, sizeof(s), "%c", tm);
strcat(s," - ");
strcat(s,ERROR_MESSAGE);
strcat(s," ");
if(strcmp(ERROR_TYPE,TEXT_FILE)==0) {
FILE *fp = fopen(FILE_PATH,"a");
fputs(s,fp);
fclose(fp);
}
else if(strcmp(ERROR_TYPE,DEBUG_CONSOLE)==0) {
fprintf(stderr,s);
}
else if(strcmp(ERROR_TYPE,OUTPUT_CONSOLE)==0) {
printf(s);
}
}
void main() {
char age[50];
printf("Enter age: ");
int count = scanf("%[^ ]", age);
if(count==0) {
ERROR_TYPE = OUTPUT_CONSOLE;
logError("input cannot be blank");
}
else if(isNumeric(age)) {
int a = atoi (age);
if(a<1||a>110) {
ERROR_TYPE = TEXT_FILE;
logError("input is out of range");
}
else
printf("Thank You!");
} else {
ERROR_TYPE = DEBUG_CONSOLE;
logError("input is nonnumeric");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.