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,
 
Joined
Aug 11, 2022
Messages
12
Reaction score
0
You are missing the curly brackets after your if statement

if (condition) {
// block of code to be executed if the condition is true
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top