0

I have tried to write this out in myriad different ways but no matter what, entering any string results in the first letter being capitalized and printed ad infinitum until i kill the terminal. i have written this using different loops and altered EACH section and subsection but cannot produce a result other than this. it compiles with make.

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

int main(void)
{
    string s = GetString();
    if(s != NULL)
    {
    for(int i = 0, n = strlen(s); i < n; i++)
    {
        if(islower(s[i]))
        {
            toupper(s[i]);
        }
        if((i = 0) || isspace(s[i - 1]))
        {
            printf("%c", s[i]);
        }
    }
    printf("\n");
    }
}

NOTE: this code may not produce the correct answer for the pset, thats fine. for the moment im just trying to get it to stop spamming me one letter so i can continue to work on it.

1 Answer 1

5
    if((i = 0) || isspace(s[i - 1]))

Are you testing for i == 0 or reassigning a value of 0 to i, i=0 ? It's a common mistake. A single = is an assignment, while two == signs is a logical test of whether two things are the same.

The result of the error is that every time you hit the if statement, you were resetting i to 0 and creating an infinite loop. You've probably been looking so hard at it that you couldn't see it anymore.

There are two types of programmers in the world, those that have made this mistake and those that won't admit to it. :-)

If this answers your question, please mark the question as answered. Let's keep up on forum housekeeping.

1
  • THANK YOU. THANK YOU. Commented May 9, 2015 at 7:15

You must log in to answer this question.

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