简体   繁体   English

我在c程序中的功能不起作用

[英]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. 我试图在我的c程序中创建2个单独的函数,第一个程序读取数组(摄影师的名字和他们的积分) ,第二个程序显示所有名称和积分。 (With printf command) (使用printf命令)

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]);
            }
        }

In your printdata() function, the variable int n; 在您的printdata()函数中,变量int n; is uninitialized. 未初始化。 The variable n here, is different than the variable n you defined inside your readdata() function. 变量n这里,大于变量不同n您在里面定义readdata()函数。 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. readdata()应该返回n,而printdata()应该将其作为参数接收。

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. 您在两个不同的函数中使用ni并没有全局定义它们,这意味着i的readdata()与print data()中的i不同 。这些是局部变量,局部变量只能在函数中访问,其中你宣布他们。 Use arguments to pass the value in printdata() which will be returned by readdata(). 使用参数在printdata()中传递值,该值将由readdata()返回。

Hope it helps. 希望能帮助到你。

Your array size ( which is n value) needs to be defined globally. 您的数组大小(为n值)需要全局定义。 As @user9849588 said, local variables are only accessible from within their respective functions. 正如@ user9849588所说,局部变量只能从它们各自的函数内部访问。

To solve this issue, you need to pass your number of photographers n to readdata and printdata functions. 要解决此问题,您需要将摄影师数量n传递给readdataprintdata函数。

#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]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM