0
#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(void)
{
    string enterword = get_string("input");
    int t = strlen(enterword);
    printf("%i\n", t);
    for(int i = 0; i<= t; i++)
    {
         int counter = 0;
         while (enterword[i] == '.')
        {
        counter = counter + 1;
        }
         printf("counter is %i",counter);
    }
}

The above is my code which is intended to count no. of full stops but fails. The program compiles successfully with counting no. of characters.

enter image description here

Revised:

#include <stdio.h>
#include <string.h>
#include <cs50.h>
int main(void)
{
    string enterword = get_string("input");
    int t = strlen(enterword);
    printf("%i\n", t);
    for(int i = 0; i<= t; i++)
    {
         int counter = 0;
         if (enterword[i] == '.') {
         counter = counter + 1;}
         printf("counter is %i",counter);
    }
}

What is intended is with counter, if full stop recorded, counter = counter + 1. But there is definitely fault in my coding. Maybe, due to placement of counter variable in the wrong place, leading to issues with local and global variable.

2 Answers 2

1

Program is in an infinite loop. When i is 6, enterword[i] == '.' evaluates to true, and the while never ends. A conditional (ie if enterword[i] == '.') is appropriate instead of a while loop.

1
  • Thanks for pointing out. I have revised my query after replacing while with if. Still my code is faulty. Could you please help. Dec 24, 2021 at 6:18
0

Got my query solved here on a forum by CS50: https://edstem.org/us/courses/176/discussion/975312

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .