write a plain C program called onceplus.c, there is a function called process wh
ID: 3698571 • Letter: W
Question
write a plain C program called onceplus.c, there is a function called process which takes one argument, a single integer, and returns an integer value.
-There are also a number of symbolic constants defined at the beginning of the file.
#define ERROR_404 'a'
#define ERROR_403 'b'
#define ERROR_400 'c'
-The process function should include a switch case using the argument as it’s condition value.
-For every symbolic constant at the beginning of the tile, there should be a separate case for that value. They should do the following:
+ For ERROR_404, the switch should print “Not Found.” on its own line.
+ For ERROR_403, the switch should print “Forbidden.” on its own line.
+ For ERROR_400, the switch should print “Bad Request.” on its own line.
Also, include a case for EOF.
For EOF, the switch should print “End of Stream” and then return 0 or FALSE.
In the default case, print “Valid input found: ” and then the argument as an ASCII character followed by a newline character.
If the argument wasn’t EOF, then the process function should return 1 or TRUE.
In the main file, write a do while loop to read one character of input from standard input at a time and pass it to the process function as an integer. Since we’re letting the process function handle EOF, don’t cast the input down to an integer just yet.
Use the process function’s return value to decide if the loop should continue as the do while’s condition.
This program should only contain ONE (1) instance of the getchar function and NO OTHER INPUT FUNCTIONS!
example output:
1 abcbacbHello, Student!
2 Not Found.
3 Forbidden.
4 Bad Request.
5 Forbidden.
6 Not Found.
7 Bad Request.
8 Forbidden.
9 Valid input found: H
10 Valid input found: e
11 Valid input found: l
12 Valid input found: l
13 Valid input found: o
14 Valid input found: ,
15 Valid input found:
16 Valid input found: S
17 Valid input found: t
18 Valid input found: u
19 Valid input found: d
20 Valid input found: e
21 Valid input found: n
22 Valid input found: t
23 Valid input found: !
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#define ERROR_404 'a'
#define ERROR_403 'b'
#define ERROR_400 'c'
int process(int val) {
int result=1;
switch(val) {
case ERROR_404:
printf("Not found ");
break;
case ERROR_403:
printf("Forbidden ");
break;
case ERROR_400:
printf("Bad Request ");
break;
case EOF: /* Press Ctrl+Z for eof */
printf("End of Stream");
result=0;
break;
default:
if(val!=' ') {
printf("Valid input found: %c ",val);
}
break;
}
return result;
}
void main() {
char val;
int continu;
do {
val=getchar(); /* Read character one by one in do-while loop*/
continu=process((int)val); /* return value of process() function will be 1 until EOF. For EOF, return value is 0*/
}while(continu!=0); /* condition will fail on EOF (i.e.,) when continu value is 0, condition will fail */
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.