Omar Khaled
Omar Khaled

Reputation: 25

scanf doesn't take all in inputs

I supposed to perform a project which allocate N bytes of memory of struct person and scanning f every person's name initial_money and some other variables the problem for me when i run the code is that it is terminating at some point of taking input process and i don't why also this problem faced me yesterday in code forces contest

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

struct person
{
    char name[15];
    int initial_money;
    int g;
    int final_money;
    int money;

};

int main()
{

    int NP,i,j;
    char target1[15];
    scanf("%d",&NP);
    struct person *p=malloc(NP*sizeof(struct person));
    for(i=0;i<NP;i++)
    {
        scanf("%s",(p+i)->name);
    }
    for(i=0;i<NP;i++)
    {

        scanf("%s",target1);

        for(j=0;j<NP;j++)
        {
            if((p+j)->name==target1)
            {
                scanf("%d%d",(p+j)->initial_money,(p+j)->g);
                (p+j)->final_money=(p+j)->initial_money%(p+j)->g;
            }
        }
    }

    for(i=0;i<NP;i++)
    {

        printf("%s %d %d %d",(p+i)->name,(p+i)->initial_money,(p+i)->g,(p+i)->final_money);
    }
    return 0;
}

Upvotes: 0

Views: 141

Answers (1)

Jo&#235;l Hecht
Jo&#235;l Hecht

Reputation: 1842

The scanf function need pointers for inputed values. The line:

scanf("%d%d",(p+j)->initial_money,(p+j)->g);

Should be:

scanf("%d %d",&(p+j)->initial_money,&(p+j)->g);

When comparing strings you usually can't compare pointers directly:

 if((p+j)->name==target1)

shoul be:

 if(strcmp((p+j)->name, target1) == 0)

Upvotes: 2

Related Questions