arrays - 如何在C中访问数组

标签 arrays c pointers

我应该输入股票名称、股票数量、购买价格和当前价格,然后计算购买总额、当前总额和利润。然后,程序应输出名称、购买总额、当前总额和利润。

输入输入后,我不断收到错误或崩溃消息:

Screenshot of error alert

(点击放大。)

这是我到目前为止所拥有的:

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

void load(char *name, float *share, float *buyprice, float *currprice)
{
    printf("Enter stock name");
    gets(name);
    printf("Enter share, buyprice, currprice");
    scanf("%f %f %f", &*share, &*buyprice, &*currprice);
}
void calc(float share, float buyprice, float currprice, float *buytotal, float *currtotal, float *profit)
{
    *buytotal = share * buyprice;
    *currtotal = share * currprice;
    *profit = *currtotal - *buytotal;
}
void output(char name, float profit, float buytotal, float currtotal)
{
    printf("%s\n", name);
    printf("buy total %f\n", buytotal);
    printf("current total %f\n", currtotal);
    printf("profit %f\n", profit);

}

void main()
{
    char name [25];
    float share, buyprice, currprice, buytotal, currtotal, profit;
    load(name, &share, &buyprice, &currprice);
    calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
    output(*name, buytotal, currtotal, profit);
    fflush(stdin);
    load(name, &share, &buyprice, &currprice);
    calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
    output(*name, buytotal, currtotal, profit);
    fflush(stdin);
    load(name, &share, &buyprice, &currprice);
    calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
    output(*name, buytotal, currtotal, profit);
    system("pause");
}

最佳答案

您遇到一些错误,您可以尝试一下:

void load(char *name, float *share, float *buyprice, float *currprice)
{
    printf("Enter stock name");
    gets(name);
    printf("Enter share, buyprice, currprice");
    scanf("%f %f %f", share, buyprice, currprice); // changes: removed &*
}
void calc(float share, float buyprice, float currprice, float *buytotal, float *currtotal, float *profit)
{
   *buytotal = share * buyprice;
   *currtotal = share * currprice;
   *profit = *currtotal - *buytotal;
}
void output(char *name, float profit, float buytotal, float currtotal) // changes: char name to char *name
{
   printf("%s\n", name);
   printf("buy total %f\n", buytotal);
   printf("current total %f\n", currtotal);
   printf("profit %f\n", profit);
}

int main(void) //changed to int main(void)
{
    char name [25];
    float share, buyprice, currprice, buytotal, currtotal, profit;
    load(name, &share, &buyprice, &currprice);
    calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
    output(name, buytotal, currtotal, profit); //changed *name to name
    fflush(stdin);
    load(name, &share, &buyprice, &currprice);
    calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
    output(name, buytotal, currtotal, profit); //changed *name to name
    fflush(stdin);
    load(name, &share, &buyprice, &currprice);
    calc(share, buyprice, currprice, &buytotal, &currtotal, &profit);
    output(name, buytotal, currtotal, profit); //changed *name to name
    return 0; //Added this line
 } 

关于arrays - 如何在C中访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572735/

相关文章:

php - 尝试从数组中删除重复的行。到栏目内容

python - 按列中的唯一值拆分 numpy 数组

c# - C# 中的可变长度数组

c++ - 由于 read() block ,无法观看多个目录

c - 指示在 c 中计算操作数的顺序

c++ - 寻找指针的来源

swift - 如何在 Swift 3 中将 UnsafePointer<[Float]> 或 Float Array 数据转换为 Unsafe[Mutable]Pointer<Float>?

c - 如何在 Pascal/Delphi 中调用以下 C 函数?

javascript - 使用 javascript 对象填充数组。通过比较两个字符串值选择对象

c - 为什么 C 标准不支持嵌套函数?