-1

I don't know why, but my code is not taking input.... where did i do mistake???

After running it just prints this:

Type your input (press enter to save and exit).️
Done, your file is saved successfully️

My code is:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE *fpp;
    char Entry;
    char sid;
    
    fpp = fopen("sid","w");
    
    if (fpp == NULL)
    {
        printf("Sorry️ file not created\n");
        exit(0);
    }
    
    printf("Type your input (press enter to save and exit).️\n");
    while (1)
    {
        putc(Entry,fpp);
        
        if(Entry =='\n')
            break;
    }
    printf("Done, your file is saved succesfully️\n");
    fclose(fpp);
    return 0;
}
  • 1
    You used no functions to read input. Also it looks like you have an extra `;` here: `if(Entry =='\n');` – MikeCAT Jun 30 '21 at 17:52
  • 5
    [`putc`](https://en.cppreference.com/w/c/io/fputc) is used for output, not input. – 0x5453 Jun 30 '21 at 17:52
  • 1
    Turn on compiler warnings. You'll be notified of a number of potential bugs, which you should try to resolve, including one that you haven't identified in your post yet. – nanofarad Jun 30 '21 at 17:54
  • @Pushp Ratan I do not see any input. – Vlad from Moscow Jun 30 '21 at 17:55
  • I'm surprised it prints the second message. `Entry` is uninitialized, so `if(Entry == '\n')` should fail and you should get stuck in an infinite loop. – Barmar Jun 30 '21 at 17:57
  • 2
    @Barmar doesn't the semicolon between `if(...)` and `break` essentially make the if a no-op? Thus `break` is executed the first time the loop runs? – erik258 Jun 30 '21 at 17:58
  • @DanielFarrell Yes, you are correct. `clang` will flag that with `-Wall`. The specific warning is: _if statement has empty body_ `-Wempty-body`. But, `gcc` needs `-Wall -Wextra` and it says: _suggest braces around empty body in an ‘if’ statement_ `-Wempty-body` – Craig Estey Jun 30 '21 at 18:10
  • @DanielFarrell For more info, see my answer: https://stackoverflow.com/questions/62232853/why-does-this-program-show-something-else-even-though-the-code-is-correct/62233171#62233171 – Craig Estey Jun 30 '21 at 18:18
  • See [this answer](https://stackoverflow.com/questions/68045486/print-line-number-incrementing-with-each-input-in-c/68058865#68058865) for help with input in C. – Nagev Jun 30 '21 at 18:21
  • `where did [I make a] mistake?` you a) didn't generate and heed all compiler warnings b) dropped the `(ch=getchar())` from your tutor's suggestion. – greybeard Jul 01 '21 at 12:28

2 Answers2

1

yes, guys I used scanf() instead of putc(). My online tutor said me to write this...

while((ch=getchar())!='\n')
    {
        putc(Entry,fpp);
    }

and I used that.... but now I used this code and it worked.

while (1)
    {
        scanf("%c",&Entry);
        
        if(Entry =='\n')
            break;
    }
  • Better `while ((ch = getchar()) != '\n' && ch != 'EOF') { ... }` which protects against the user generating a manual `EOF` to cancel input. – David C. Rankin Jul 01 '21 at 06:12
  • Please move the part about your tutor's direction from answer to question. What happened to the `(ch=getchar())` part? – greybeard Jul 01 '21 at 12:24
0

In order to get this out of the list of unanswered questions I compile an answer from comments.
Credits to the commenters: MikeCAT, 0x5453, Vlad, Daniel Farrell. Other commenters provided wise input, which I however do not see as immeditate parts of the solution.

Your question is not very specific about what puzzles you about the shown output so here is my guess, along with explanation:

  • Why does any input you provide not have any influence on program behavior?

Because you do never take any input. It seems that you intend to read input with putc(Entry,fpp);, but as 0x5453 comments

putc is used for output, not effective input.

I.e. with no input reading functions called there is no input.

  • Why does Done, your file is saved successfully️ occur at all? It should only occur after '\n' input.

Because you have an unconditional break; in your endless loop.
That might be non-obvious, but as Daniel Farrell commetns:

... the semicolon between if(...) and break essentially makes the if a no-op... Thus break is executed the first time the loop runs.

I.e. unconditional immediate break, end of loop, output. End of program.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • All commenters, let me know if I misrepresent your input or you want me to remove it from my answer. – Yunnosch Jun 30 '21 at 19:15