My program is supposed to erase /**/ comments, condense white spaces, and remove line splices of the input.
**edit: scroll down to problem update below.
Here is my code:
#include <stdio.h>
void ass(), f1(), f2(), f3();
int mainRunner();
int a, b;
int main() {
ass();
mainRunner();
}
void ass() {
a = getchar();
b = getchar();
}
int mainRunner() {
while ( a != EOF ) {
f1();
f2();
f3();
putchar(a);
a = b;
b = getchar();
}
}
// Removes Line Splices
void f1() {
if ((a == '\\') && (b == '\n')) {
a = getchar();
b = getchar();
mainRunner();
}
}
//Removes Comments in the /*...*/ form
void f2() {
if ((a == '/') && (b == '*')) {
while ((a != '*') || (b != '/')) {
a = b;
b = getchar();
}
a = getchar();
b = getchar();
mainRunner();
}
}
//Condenses White Spaces
void f3() {
if ((a == ' ') && (b == ' ')) {
a = b;
b = getchar();
mainRunner();
}
}
When I run the testscript (testscript 1):
a b c
d e
f
g
hifealkfja;efa faekjf;ale feafaefa
This is returned
a b c
d e
f
g
hifealkfja;efa faekjf;ale feafaefa
????????????????
When I run this testscript (testscript 2):
start linesplice NOW!\
This should be connected with first line.Comment begins here:/*fjelajfal;efjael$
fe;ajfe;fe8/Comment Ends.
Series of 5 spaces between the letters:a b
Series of 10 spaces between the letters:c d
Increasing number of spaces between letters from 1 space:e f g h i
Nothing happens and the command line cursor goes to the very left.
I appreciate any feedback, thanks. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Problem Update: Reduced the issue above by doing what chux suggested. Now I only have the question mark problem. I made a fix so that putchars wouldn't be waiting in the stack and output a whole bunch of EOFs at the end, but why are there still two question marks? Here's my new code:
#include <stdio.h>
void ass();
int mainRunner();
int a, b;
int main() {
ass();
mainRunner();
}
void ass() {
a = getchar();
b = getchar();
}
int mainRunner() {
while ( a != EOF ) {
// Removes Line Splices
if ((a == '\\') && (b == '\n')) {
a = getchar();
b = getchar();
mainRunner();
}
//Removes Comments in the /*...*/ form
if ((a == '/') && (b == '*')) {
while ((a != '*') || (b != '/')) {
a = b;
b = getchar();
}
a = getchar();
b = getchar();
mainRunner();
}
//Condenses White Spaces
if ((a == ' ') && (b == ' ')) {
a = b;
b = getchar();
mainRunner();
}
if (a == EOF) {
break;
}
else {
putchar(a);
a = b;
b = getchar();
}
}
}
When I run the second test script from above (with an asterisk added to end the comment), my program outputs:
start linesplice NOW!This should be connected with first line.Comment begins here:Comment Ends. Series of 5 spaces between the letters:a b Series of 10 spaces between the letters:c d Increasing number of spaces between letters from 1 space:e f g h i ??
Notice the two question marks at the end. Note: the program works fine for the first test script.
Given OP's first testscript:
The if
in f1()
is never true.
The if
in f2()
is never true.
f3()
effectively recurses into itself via mainRunner()
when multiple space are encountered. Upon seeing EOF
, the recursion stops, and unwinds. mainRunner()
performs putchar(a);
with EOF in a
with each unwinding.
Removing mainRunner();
in f1()
, f2()
, & f3()
may help.
The test code has 1 + 13 + 1 + 2 sequences of 2 spaces. This causes 17 recursive calls. Upon unwinding putchar(EOF)
is called 16 times creating the '?'
.
In OP's 2nd testscript. f2()
gets into an infinite loop with
while ((a != '*') || (b != '/')) {
a = b;
b = getchar();
}
as there is no escape should getchar()
return EOF
.