I' working on an array where the user inputs characters(up to 80). I then need to output the characters, one per line, until the string is null. My problem is in the while loop i think, im just not sure what to do from here. Any ideas?

#include <stdio.h>
void main()
{
char chararray[80];
int n = 0;
	
printf("Insert a string no greater than 80 characters. Press enter to end string.\n");
gets(chararray); 
		
while (chararray[n] != NULL, chararray[n++]);
{
 	printf("%c", chararray[80]);
}

printf("Thank you for using this program\n");
return;
}

Edited 6 Years Ago by Nick Evan: Added code-tags. Removed custom line-numbers BY HAND

I think u r very new to programming as u have done silly mistakes...
Look at the while loop at lines 10 to 13 :

10.while (chararray[n] != NULL, chararray[n++]);
11.{
12. printf("%c", chararray[80]);
13.}

chararray[n++] does not make any sense. U need to increment 'n' to point to the next character, which u can do inside the loop. Check out the semi colon [;] at the end of the line (It is ruining ur whole loop)
Then u are trying printing only the 80th character of the array only. Is that what u want ???
Replace the loop with :

while (chararray[n] != NULL)
{
    printf("%c\n", chararray[n]);
    n++;
}

And also make a note of what gerard4143 has pointed out...
Happy programming ;)

I'm working on an array where the user inputs characters(up to 80). I then need to output the characters, one per line, until the string is null. My problem is in the while loop i think, im just not sure what to do from here. Any ideas?

1. #include <stdio.h>
2. void main()
3. {
4. char chararray[80];
5. int n = 0;
6.  
7. printf("Insert a string no greater than 80 characters. Press enter to end string.\n");
8. gets(chararray); 
9.      
10.while (chararray[n]>80)
11.{
12.     printf("%c", chararray[n]);
13.
        n++; 
    }

14.
15.printf("Thank you for using this program\n");
16.return;
17.}

end quote.

ans- never use ; after any loop or condition...

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi mohd.shadab14,
Though ur intention was correct as per the problem, but the statement is somehow incorrect !!!
That's because there are cases when we intentionally use finite loops terminated by a semicolon ;
That approach will be "Do nothing or wait, until the condition is met". There the condition might be dependent on some other external resources or applications behavior. This mainly helps in case of synchronization.

There are plenty of other cases where you might have a loop with no body. Any calculation or algorithm where the loop actually does the calculation, for instance finding the end of a singly linked list

// Should check head is not NULL before this
for(current=head; current->next != NULL; current = current->next)
    ;
// Current now points to last entry in list

Also note that there is a semi-convention to put intentional ; following a loop on the next line of code.

In fact some compiles will produce a warning for a ; on the same line as the loop but not for a ; on the next line.

True enough Banfa...
But have you really met with a compiler warning for mentioning ; in the same line ?? (If so can u mention the warning)
Because compiler interprets ; as a single empty statement (whether in the same or next line)

While I seem to be certain that there are compilers that do this none of the ones I current use (mingw and Visual C++) do.

Lint does though.

Yeah, but Lint is a special tool (more than a compiler) made to check for potential errors in the code.
I checked with GCC also, without a warning !!! But I have used "Telelogic Logiscope" for Code Review, where we get to find all sorts of possible warnings by the implemented rules, to be checked for code validation.

This article has been dead for over six months. Start a new discussion instead.