0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct org
{
    char name[40];
    int profit;
    int persono;
};

int main()
{
    struct org *o;
    int i,n;
    printf("How much org :- ");
    scanf("%d",&n);
    o=(struct org*)calloc(n, sizeof(struct org));
    for (i=0; i<n; ++i)
    {
        printf("Give the Name of Organization - ");
        fgets((o+i)->name,sizeof((o+i)->name),stdin);
        (o+i)->name[strcspn((o+i)->name,"\n")]='\0';
        printf("Name of company is :- %s",(o+i)->name);
    }

    free(o);
    return 0;
}

The first loop isn't working please help out. I used fgets for this and but its working with scanf. And my question is Short stack overflow is making me write these lines too. Very Disgusting.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    Please get out of the habit of writing `(o+i)->name`. The usual way to write this is `o[i].name` – Barmar Feb 03 '22 at 17:22
  • 1
    What do you mean by "not working"? And why can't you use `o[i]` notation? Would make it much more readable. – littleadv Feb 03 '22 at 17:23
  • Try adding a newline to the end of your printf. – Barmar Feb 03 '22 at 17:24
  • The lesson is: don't mix the input methods until you are familiar with each of their idiosyncrasies, and know how to compensate. Use `fgets` and `sscanf` (or other) to input `n`. – Weather Vane Feb 03 '22 at 17:25
  • If I just want the code to run, there are various ways to do it. But I want to get it using (o+i)->name. – Shashank Singh Feb 03 '22 at 17:45

0 Answers0