PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] how to create each word of input to start a new line

 October 30, 2022     c, eof, getchar, putchar     No comments   

Issue

Hey i am trying to print each word on a new line. My EOF is also not working and was wondering why this is. i have made it to scan for a space and then print new line.

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

int main(void)
{
    char ch;

    while ((ch = getchar()) != '#')
        putchar(ch);

int nextChar;
    nextChar = getchar();
    while (nextChar != '\n' && nextChar != EOF);
    {
        if (ch== ' ')
        {
            printf("\n");
        }
        else
        {
            putchar(ch);
        }
        {
            ch = getchar();
        }
        printf("\n");

        {
            scanf("%lc",&nextChar);
            printf("%c",nextChar);
        }

        return 0;
    }
}

just for example input: Stackoverflow is great
output:
Stackoverflow
is
great


Solution

You should REALLY start enabling compiler warnings. They can help you find many bugs. Look here when I compile with -Wall and -Wextra.

$ gcc ba.c -Wall -Wextra
ba.c: In function ‘main’:
ba.c:13:5: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
     while (nextChar != '\n' && nextChar != EOF);
     ^~~~~
ba.c:14:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
     {
     ^

Remove the ; after the while loop.

But there are other problems too. As you can see from when I corrected the indentation for you, the return 0 statement is inside the while loop. I assume that's not what you want.



Answered By - klutt
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing