MrsDalloway
MrsDalloway

Reputation: 1

my function inside of c program does not work

I am trying to create 2 separate function in my c program that First program reads the arrays ( names of photographers and their points) and the second one displays all names and points. (With printf command)

But the program doesn't run my second function. What is wrong with my function?

Thanks in advance

    #include <stdio.h>
`#include <string.h>`

    void readdata(char name[15][15],float points[15]);
    void printdata(char name[15][15],float points[15]);

int main () 
{
    char names[15][15];
    float points[15];




        readdata(names,points);
        printdata(names,points);



    return 0;

}

    void readdata(char name[15][15],float points[15])   
    {
        int i;
        int n;

        printf("Please enter the number of photographers ( The value should be less than 15)\n");
        scanf("%d",&n);
        while(n<0 || n>15)  
        {
            printf("PLEASE ADD NUMBER BETWEEN 1 AND 15\n");
            scanf("%d",&n);
        }

        for(i=0; i<n;i++)   
        {
            scanf("%s%f", name[i],&points[i]);

        }
    }


    void printdata(char name[15][15],float points[15])
        {

            int i;
            int n;
            for(i=0; i<n;i++) 
            {
                printf("%s\t", name[i]);
                printf("%.f\n", points[i]);
            }
        }

Upvotes: 0

Views: 55

Answers (3)

Sandipan
Sandipan

Reputation: 703

You are using n and i in two different functions and not defining them globally means the i the readdata() is not the same as the i in print data().These are the local variables and local variables are only accessible within the function where you declared them. Use arguments to pass the value in printdata() which will be returned by readdata().

Hope it helps.

Upvotes: 1

isydmr
isydmr

Reputation: 657

Your array size ( which is n value) needs to be defined globally. As @user9849588 said, local variables are only accessible from within their respective functions.

To solve this issue, you need to pass your number of photographers n to readdata and printdata functions.

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

void readdata(char name[15][15],float points[15], int n);
void printdata(char name[15][15],float points[15], int n);

int main () 
{
    char names[15][15];
    float points[15];
    int size;
    printf("Please enter the number of photographers ( The value should be less than 15)\n");
    scanf("%d",&size);
    while(size<0 || size>15)  
    {
        printf("PLEASE ADD NUMBER BETWEEN 1 AND 15\n");
        scanf("%d",&size);
    }
    readdata(names,points,size);
    printdata(names,points,size);



return 0;

}

void readdata(char name[15][15],float points[15],int n)   
{
    int i;

    for(i=0; i<n;i++)   
    {
        scanf("%s%f", name[i],&points[i]);

    }
}


void printdata(char name[15][15],float points[15],int n)
    {

    int i;
    for(i=0; i<n;i++) 
    {
        printf("%s\t", name[i]);
        printf("%.f\n", points[i]);
    }
}

Upvotes: 0

user9849588
user9849588

Reputation: 607

In your printdata() function, the variable int n; is uninitialized. The variable n here, is different than the variable n you defined inside your readdata() function. These are local variables and are only accessible from within their respective functions.

readdata() should return n and printdata() should receive it as an argument.

Upvotes: 2

Related Questions