Do while loop problem

+1 Guy 99 · January 1, 2015
I have made a program to count the number of characters a user enters. But due to some problem which i dont know about, in every second loop it doesnt wait for the user input. It still counts that loop and adds it to the total count.

Below is the source code.

--------------------------------------------------------------------------------------------------------------



#include <stdio.h>
#include <stdlib.h>

int main()
{
int total=0;
char storage;

printf("Press z when finished entering\n\n");

do
{
    printf("\nEnter your single character: ");
    scanf("%c",&storage);
    total++;

}while(storage!='z'&&storage!='Z');
total--;
printf("The total is %d",total);

return 0;

}



--------------------------------------------------------------------------------------

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · January 1, 2015
scanf is a strange function

Replace
scanf("%c", &storage);

with
scanf(" %c", &storage);

The withspace before the % will fix it.
0 c student · January 2, 2015
the problem in this case with your scanf is that you are reading in only one character at a time.  once this is looped, it can cause an issue with inputhttp://i.imgur.com/GTUGVkb.png
 allow me to explain what this image is showing.  when you enter an input to scanf, it will take in all characters until a newline or eof character but in your scenario, it is special...  when you press enter to "submit" an input, you are also entering another character, this is called the newline character, and yes, it is a character.  the reason it "duplicates" is because you are reading in only one character at a time, so when i enter in the character 'a', it is technically 'a' + the newline character '\n', meaning that when you loop it, it will take in the next character from input, which is '\n', thus explaining your problem here.

your solution?
you can try adding something to "absorb" the newline character or use another function.
0 Laura Lee · January 3, 2015
I would avoid using scanf completely. 
Most people don't realise that there are still carriage returns inside the input stream. This can cause frustration, especially when inputting a single character where '\n' is a single character. 
Which is why you should use fgets or fgetc, then take care of the buffer or stream yourself.
#include <stdio.h>
int main()
{
    char c = 0;
    while(c != 'Z' && c != 'z')
    {
        printf("Enter a signle character:");
        c = fgetc(stdin);
        printf("Character entered %c\n",c);
        fgetc(stdin);//Flush out the carriage return
    }
    return 0;
}
0 Laura Lee · January 4, 2015
The value returned by both getchar and fgetc are 'promoted' to integer values. There is literally zero difference between getchar and fgetc except for the fact that fgetc is more flexible in allowing a file object. 

I'm well aware of the return values of both getchar and fgetc. For simplicity sake I didn't use a char instead of an int. Why? Because my file object was the input stream itself. I wasn't going to give it an EOF code and I wasn't reading from a file which could return an error that I wouldn't be able to debug because its value was greater than 255. This was a controlled situation with a controlled piece of code.

As for Franz Schmidt solution, I don't disagree with it; however, I'd much rather use fgets which still store the carriage return inside the buffer and I can fix it myself.

Even fgets has its issues in the case I enter a size to correctly accommodate for the the buffer, I'll still have data left over at the input stream. Hence why I use my own function to correctly handle the stream regardless of the buffer/size passed to it. 
  • 1

C

103,800 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator