C basic query


Joined
Aug 1, 2022
Messages
1
Reaction score
0
Hello All,

I made a simple code to count the number of 'a' until meet '.'

Below is the code that I programmed.
=============================================================
int i = 0;
char input ='0';


while (input != '.')
{
if (input == 'a')
i++;
printf("Enter your character : ");
input = getchar();

}

printf("number of a : %d", i);
============================================================

Unfortunately, the result is not what I intended.
===========================================================
Enter your character : a
Enter your character : Enter your character : ab
Enter your character : Enter your character : Enter your character : abc
Enter your character : Enter your character : Enter your character : Enter your character : .
number of a : 3
========================================================

I can't understand the reason why "Enter your character" is printed repeatedly.

Please help me.

Thanks,
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top