Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program using C language Compile and run the program questions.c. It rea

ID: 649542 • Letter: W

Question

Write a program using C language

Compile and run the program questions.c. It reads in a line from the user and determines whether it is a yes or no based on the first character on the line. The program uses for loops (an "infinite" for loop and another "for" loop that is missing its initialization parts). Rewrite it using "while" loops instead.

/*      File : questions.c                 */

/*
* Ask the user a question.
*/
#include <stdio.h>
#include "tfdef.h"

int main()
{
        printf("Don't you just love this class? ");
        if (yesOrNo())
                printf("YES! We knew it. ");
        else
                printf("NO? Come on, you know you love this class. ");

}

int yesOrNo(void)
{
int answer;                        /* holds input character */
int c;

        for (;;)
        {
                /* process each input line */

                c = getchar();
                if ((answer = tolower(c)) == EOF)
                        return FALSE;                  /* EOF is NO! */

                /* read characters until the end of the line */
                for (; c != ' ' && c != EOF; c = getchar());

                /* 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: ");
        }
}

To help you figure out what's going on, questions2.c is the same program, but with printf statements that say exactly what's going on. After you get questions2.c working using "while" loops, delete all of its debugging statements.

/*      File : questions2.c                  */

/*
* Ask the user a question.
*/
#include <stdio.h>
#include "tfdef.h"

int main()
{
        printf("Don't you just love this class? ");
        if (yesOrNo())
                printf("YES! We knew it. ");
        else
                printf("NO? Come on, you know you love this class. ");

}

int yesOrNo(void)
{
int answer;                        /* holds input character */
int c;

        for (;;)
        {
                /* process each input line */

                printf("debug: Processing New Input Line ");

                c = getchar();
                printf("debug: Read First Character: %c ", c);
                if ((answer = tolower(c)) == EOF)
                        return FALSE;                  /* EOF is NO! */

                printf("debug: The answer is: %c ", answer);

                /* read characters until the end of the line */
                for (; c != ' ' && c != EOF; c = getchar())
                        printf("debug: Skipping character: %c ", c);

                /* 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: ");
        }
}

Explanation / Answer

/*      File : questions.c      *
*      By   :                  *
*      login:                  *
*      team :                  *
*      Date :                  */
/*
* Ask the user a question.
*/
#include <stdio.h>
#include "tfdef.h"
int main()
{
        printf("Don't you just love this class? ");
        if (yesOrNo())
                printf("YES! We knew it. ");
        else
                printf("NO? Come on, you know you love this class. ");
}
int yesOrNo(void)
{
int answer;                        /* holds input character */
int c;
        while (TRUE)
        {
                /* process each input line */
                c = getchar();
                if ((answer = tolower(c)) == EOF)
                        return FALSE;                  /* EOF is NO! */
                /* read characters until the end of the line */
               while (c != ' ' && c != EOF){
               c = getchar();
               };

                /* 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: ");
        }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote