gpt4 book ai didi

arrays - 如何在C中访问数组

转载 作者:行者123 更新时间:2023-11-30 21:21:06 25 4
gpt4 key购买 nike

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

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com