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?

Recommended Answers

All 7 Replies

Are you really sure you want your while to read like below? Maybe you mean while (true) or while(1)

while ('\n')

for (;;) just means "forever". typically used for a loop that will execute an indeterminate number of times, exiting with a break; command once the exit condition is found.

the corresponding while would be while(1) or while(TRUE) ... doesnt make any sense to use '\n', even though technically '\n' is non-zero and it will work. it just looks really weird and implies that the programmer doesn't know what they're doing.

the second for loop is a bad example. this will be a source of problems. i can't just convert this to a while loop, because the while loop will then be faulty.

think about what that for loop is doing.... what will it execute if the DEBUG is defined? what will the for loop execute if the DEBUG is not defined? see the problem?

in any event, you'll want to get the character 'c' first, then evaluate it if it is '\n' or EOF to decide whether to conditionally execute the loop or exit.. how you structure your loop may also depend on what you're doing with 'c'

.

In general

for (i=0; i<10; i++)
{
    ....
}

becomes

i=0;
while (i < 10)
{
    ....
    i++;
}

In other syntax:

for ( A; B; C)
{
}

becomes

A;
while(B)
{
    C;
}

I got the first for loop down. However, I'm having problems with the second one. It compiles, and I get the correct output. But, the debug lines are different if I were to use the for loop instead.

The second loop is

1.
      for (; c != '\n' && c != EOF; c = getchar())

With a while loop, the code is:

'\n';
                 while (c != '\n' && c != EOF)
                 {
                          #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;

                   c = getchar();
                 }

                /* error message if there's a problem */
                printf("Please answer with a YES or NO: ");

When I entered "yes" at input, these were the results.

Don't you just love this class? debug: Processing New Input Line
yes
debug: Read First Character: y
debug: The answer is: y
debug: Skipping character: y
YES! We knew it.

This is the result that I want:

Don't you just love this class? debug: Processing New Input Line
yes
debug: Read First Character: y
debug: The answer is: y
debug: Skipping character: y
debug: Skipping character: e
debug: Skipping character: s
YES! We knew it.

What's wrong with my code?

for one thing you're getting characters into 'c' and parsing results based on something called 'answer'

Where did you check for 'e' and 's'? There is only a check for 'y'.

I see the problem now. Thank for everyone's help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.