3

I am new to C programming. And I was doing a practice, and the question goes like this: Use the ?: operator and the for statement to write a program that keeps taking the characters entered by the user until the character q is accounted.

And here is the program I wrote:

#include <stdio.h>

main()
{
    int x, i=0;
    for (x = 0; x == 'q'? 0 : 1; printf("Loop %d is finished\n",i))
    {
        printf("Enter q to exit!!!\n");
        printf("Please enter a character:\n");
        x=getc(stdin);
        putc(x,stdout);
        ++i;
    }
    printf("\nThe for loop is ended. Bye!");

    return 0;       
}

The problem is: every time I enter a "non-q" character, the loop seems to run twice. I don't know what is wrong with my program. Please help!

David
  • 15,894
  • 22
  • 55
  • 66
MechAvia
  • 343
  • 1
  • 4
  • 13
  • 2
    seems like you need to clear the input buffer, because you're reading in a newline I think. – zeitue Apr 20 '14 at 06:14

4 Answers4

4

When you enter a non-q letter, you also hit Enter, which is read in the second loop.

To make the loop only run once per input, use fgets() to read an entire line of input at once, and check if the input string matches your expectations.

jxh
  • 69,070
  • 8
  • 110
  • 193
3

When you type a and then press Enter, the newline character becomes part of the stdin stream. After a is read, the next time you execute x=getc(stdin), the value of x is set to \n. That's why two iterations of the loop get executed.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

The loop runs twice because when you enter a non-q character, you actually enter two characters - the non-q character and the newline '\n' character. x = getc(stdin); reads the non-q character from the stdin stream but the newline is still lying in the buffer of stdin which is read in the next getc call.

You should use fgets to read a line from the stream as others have suggested and then you can process the line. Also, you should specify the return type of main as int. I suggest the following changes -

#include <stdio.h>

int main(void)
{
    int x, i = 0;

    // array to store the input line
    // assuming that the max length of
    // the line is 10. +1 is for the 
    // terminating null added by fscanf to
    // mark the end of the string
    char line[10 + 1];

    for (x = 0; x == 'q'? 0 : 1; printf("Loop %d is finished\n", i))
    {
        printf("Enter q to exit!!!\n");
        printf("Please enter a character:\n");

        // fgets reads an input line of at most 
        // one less than sizeof line, i.e., 
        // 10 characters from stdin and saves it 
        // in the array line and then adds a 
        // terminating null byte
        fgets(line, sizeof line, stdin);

        // assign the first character of line to x
        x = line[0];
        putc(x, stdout);
        ++i;
    }
    printf("\nThe for loop is ended. Bye!");

    return 0;       
}
ajay
  • 9,402
  • 8
  • 44
  • 71
1

When you enter a character, say 'x' and press enter, you actually input two characters, which are 'x' and '\n' also known as newline(when you hit enter). The '\n' becomes part of the input stream and the loop is executed for it as well.

Also, try inputting "xyz" and hit enter, the loop will be executed 4 times. For each 'x', 'y', 'z', and '\n'.

If you want the code to work one time for every input, use the function gets.

#include <stdio.h>

main()
{
    int i=0;
    char x[10];
    for ( ; x[0]!='q'; printf("Loop %d is finished\n",i) )
    {
        printf("Enter q to exit!!!\n");
        printf("Please enter a character:\n");
        gets(x);
        i++;
    }
    printf("\nThe for loop is ended. Bye!");

    return 0;
}

In this code we declared x as an string, the gets() function reads the whole line we entered, then in the condition part of the for loop, we check whether the first character of the string is 'q' or not.

nishantbhardwaj2002
  • 757
  • 2
  • 6
  • 18