Originally Posted by
bithub
Only if you want new_name->title[0] to always equal zero. You should be using a do/while loop here because you want to perform an action before you check the condition.
I don't think a do while loop is the answer, unless he wants to store garbage in the first spot. A while loop would work fine, but he needs to read the first character before entering the loop body:
Code:
while ((c1 = getchar()) != '\n' && i < 21)
{
new_name->title[i++] = c1; // this increments i after assigning to title
}
or use fgets:
Code:
fgets(new_name->title, 21, stdin);
// fgets stores the newline, so we will trim it if it's there
if (new_name->title[strlen(new_name->title)] == '\n') {
new_name->title[strlen(new_name->title) = '\0';
}