0

My guess is that it has to do with (s[i] == ' ') and I don't know what other syntax I could use.

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

int main(void)
{
string s = GetString();

printf("%c", toupper(s[0]));

for (int i = 0, n = strlen(s); i < n; i++)
{

    int x = i;

    while (s[i] == ' ')
    {
        printf("%c", toupper(s[x + 1]));
    }

printf("/n");
}
}

1 Answer 1

1

An infinite loop happens if the while condition is never false. Be the computer. What is going to happen if your input is "a miller"? The first space is encountered when i is 1. Let's do the loop:

s[1] = ' ' ? True, print.
s[1] = ' ' ? True, print.
s[1] = ' ' ? True, print.

ad infinitum (literally!).

Remember, from the loops short, that loops are used when "we have one thing we want to do multiple times,". Maybe if we change that to "we have one thing we want do do multiple times before we do the next thing" it would make the concept clearer. It is true we want to test for space multiple times, but we only want to test each character once, before moving on to the next character. So we really are looking for the "condition" of each character. An if test would be the appropriate construct.

Once you fix this problem, other problems may (will!) become apparent, that's the joy of coding!

1
  • Thanks for the answer!
    – Cristian
    Oct 16, 2016 at 20:58

You must log in to answer this question.

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