0

im trying to write this code in a particular way but my programme seems to keep getting the letter counts wrong.

running debug 50 it seems that the bool i have created (letter) does not change from true to false during iterations.

is there something wrong in my synatax that is preventing this from happening? any other help with my code is also appreciated.

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
    // DECLARE LETTER, WORD & SENTENCE COUNT

    int letters = 0;
    int words = 1;
    int sentences = 0;

    // DECLARE THE USERINPUT AND ITS COUNTER
    string userinput;
    int checkinput;

    //OBTAIN A VALID INPUT
    do
    {
        userinput = get_string("input text: ");
        checkinput = strlen(userinput);
    }
    while (checkinput <= 0);

    // CREATE BOOLEAN EXPRESSION FOR IDENTIFYING LOWERCASES, UPPERCASES, WORDS AND SENTENCES

    int lettersIterate = 0;
    bool letter = (isalpha(userinput[lettersIterate]));

    // ITERATE THROUGH THE TEXT TO FIND LETTER COUNT
    for (; lettersIterate <= checkinput; lettersIterate++)
    {
        if (letter == true)
        {
            letters++;
        }
        if (userinput[lettersIterate] == ' ')
        {
            words ++;
        }
        if (userinput[lettersIterate] == '.' || userinput[lettersIterate] == '?' || userinput[lettersIterate] == '!')
        {
            sentences++;
        }
    }
    printf("letters %i\n", letters);
    printf("words %i\n", words);
    printf("sentences %i\n", sentences);
}
0

The value of letter will never change. The code is setting it before the loop and there is nothing inside the loop that modifies it. Code that executes before a loop do not update vars inside a loop. You could fix this by putting your test inside the loop at an appropriate location.

However, there's a better way that eliminates lettersIterate and letter.

for (int i = 0, ; i < checkinput; i++)
{
    if (isalpha(userinput[i]))
    {
        letters++;
    }
    if (userinput[i] == ' ')
    { ...

This eliminates extraneous vars and simplifies the code. Also, google isalpha() and related library functions and get familiar with them. I find the TutorialsPoint website to be a good resource. Learn to go looking for library functions.

If something makes you think "Hey, this looks like something that's a really common thing to do!" then you should go looking for a function that already exists. It probably does! Google is your friend!

Also, please spend some quality time with style50. There's entirely too much whitespace in the code, so I edited it to pare it down.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • thanks for the help Cliff B. I thought globally declaring variables is a good idea but it seems scope is more specific then i interperated. also thanks for the website link. lots of info on there :) – Pot Noodle Jun 15 at 21:58
  • Yes, scope can be very specific. In short, a variable only exists inside the closest set of brackets surrounding it. As for "global variables", you haven't set any in this code. A global variable is declared outside of main or any other function. You'll learn about those later. Or, you can google it. – Cliff B Jun 16 at 4:29

You must log in to answer this question.

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