I have two for loops that I would like to convert into while loops instead. This is the entire code of the program. This program simply asks a questions and determines whether the input is a yes or no based on the first character of the line.
#include <stdio.h>
#include "tfdef.h"
#define DEBUG
int main()
{
printf("Don't you just love this class? ");
if (yesOrNo())
printf("YES! We knew it.\n");
else
printf("NO? Come on, you know you love this class.\n");
}
int yesOrNo(void)
{
int answer; /* holds input character */
int c;
for (;;)
{
/* process each input line */
#ifdef DEBUG
printf("debug: Processing New Input Line\n");
#endif
c = getchar();
#ifdef DEBUG
printf("debug: Read First Character: %c\n", c);
#endif
if ((answer = tolower(c)) == EOF)
return FALSE; /* EOF is NO! */
#ifdef DEBUG
printf("debug: The answer is: %c\n", answer);
#endif
/* read characters until the end of the line */
for (; c != '\n' && c != EOF; c = getchar())
#ifdef DEBUG
printf("debug: Skipping character: %c\n", c);
#endif
/* return an appropriate value for Yes or No */
if (answer == 'y')
return TRUE;
if (answer == 'n')
return FALSE;
/* error message if there's a problem */
printf("Please answer with a YES or NO: ");
}
}
The first for loop is:
for (;;)
and my conversion is
while ('\n')
The second for loop is :
for (; c != '\n' && c != EOF; c = getchar())
and my conversion is
while ('\n')
{
if (c!= '\n' && c!= EOF)
{
c = getchar()
}
}
Are my answers correct?