我正在做C作业并尝试让这个程序运行,但我明白了:
任何人都可以运行这个,看看你是否得到相同的? (更改文本文件的输出目录)
我已经尝试了很长时间而且不能这样做:
以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
/////////////////
#define SIZE 2
/////////////////
struct Stock
{
char name[10];
int numShares;
float buyShare,currPrice,fees;
float initCost,currCost,profit;
};
/* Load the data from the keyboard and calculates
the Initial Cost, Current Cost, and Profit
for each stock via an array */
void load(struct Stock s[], int n)
{
char nameCompare[30] = "Open the bay doors";
int i;
for(i=0;i<n;i++)
{
printf("Enter the Stock Name\n");
printf(">");
gets(s[i].name);
/////////////////////////////////////
if(strncmp (s[i].name,nameCompare,10) == 0)
{
printf("\tI'm sorry, Dave, I'm afraid I can't do that\n");
Sleep(3000);
exit(1);
}
/////////////////////////////////////
printf("Enter the Number of Shares\n");
printf(">");
scanf("%d", &s[i].numShares);
printf("Enter the Buying Price Per Share\n");
printf(">");
scanf("%f", &s[i].buyShare);
printf("Enter the Current Price Per Share\n");
printf(">");
scanf("%f", &s[i].currPrice);
printf("Enter the Yearly Fees\n");
printf(">");
scanf("%f", &s[i].fees);
s[i].initCost = (float)s[i].numShares * s[i].buyShare;
s[i].currCost = (float)s[i].numShares * s[i].currPrice;
s[i].profit = s[i].currCost - s[i].initCost - s[i].fees;
fflush(stdin);
}
}
/* Sort the array of structures
on stock name and print the array
after the sort is completed */
void sort(struct Stock s[], int n)
{
Stock t;
for(int i=0;i<n-1;i++)
for(int j=0;j<n-1;j++)
if(strcmp(s[j].name, s[j+1].name)>0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
/* Calculate and print the total profit for all of the stocks.
That is, find the sum of the 5 profits for each stock. In
addition, find and print out the number of stocks that
had a positive profit, the number of stocks that had a
negative profit, and the number of stocks that broke
even, that is had a profit of $0.00 */
void calc(struct Stock s[],int n)
{
float total=0;
int Pos=0;
int Neg=0;
int Even=0;
for(int i=0;i<n;i++)
{
total +=s[i].profit;
if (s[i].profit>0)
++Pos;
else if (s[i].profit<0)
++Neg;
else
++Even;
}
printf("\n");
printf("%d of stocks broke Positive\n",Pos);
printf("\t%d of stocks broke Negative\n",Neg);
printf("\t\t%d of stocks broke Even\n",Even);
printf("\n");
printf("The Total Trofit is $%f\n", total); //Check output
printf("\n");
}
//Output of the calc function
void print(struct Stock s[], int n)
{
for(int i=0;i<n;i++)
{
printf("\n");
printf("The stock is %s\n", s[i].name);
printf("\tWith Initial cost of $%0.2f\n", s[i].initCost);
printf("\t\tCurrent cost is $%0.2f\n", s[i].currCost);
printf("\t\t\tAnd your Profit is $%0.2f\n", s[i].profit); //Check output
printf("\n");
}
}
//Save the array of structures to a text file.
void savetext(struct Stock s[], int n)
{
FILE *f;
f = fopen("c:\\cs36\\textfile.txt","w");
int i;
for(i=0;i<n;i++)
{
fprintf(f,"%s\n",s[i].name);
fprintf(f,"%d %f %f %f %f %f %f\n", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
}
fclose(f);
}
//Retrieve and print the text file.
void loadtext(struct Stock s[], int n)
{
FILE *f;
f = fopen("c:\\cs36\\textfile.txt","r");
for(int i=0;i<n;i++)
{
fgets(s[i].name, sizeof(s[i].name), f);
fscanf(f, "%d%f%f%f%f%f%f\n", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
}
fclose(f);
}
//Save the array of structures to a binary file.
void savebin(struct Stock s[], int n)
{
FILE *f;
f = fopen("c:\\cs36\\binfile.bin","wb");
if(! f)
{
printf("Could not open the file");
exit(1);
}
else
fwrite(&s, sizeof(s[10]), n, f);
fclose(f);
}
//Retrieve and print the binary file.
void loadbin(struct Stock s[], int n)
{
FILE *f;
f = fopen("c:\\cs36\\binfile.bin","rb");
if (! f)
{
printf("Could not open the file");
exit(1);
}
else
fread(&s, sizeof(s[10]), n, f);
fclose(f);
}
int main (void)
{
printf("Hello, Dave\n");
Stock s[SIZE];
load (s, SIZE);
sort (s, SIZE);
savetext (s, SIZE);
savebin (s, SIZE);
print (s, SIZE);
calc (s, SIZE);
loadtext (s, SIZE);
print (s, SIZE);
loadbin (s, SIZE);
print (s, SIZE);
system("PAUSE");
}
这是C,使用Visual C ++ 2008 Express
答案 0 :(得分:3)
您在调用NULL
的某个地方传递了printf
指针。它很可能是格式字符串或字符串参数(%s
)。
点击调试按钮,可视化调试器将准确显示错误调用源代码的位置。
答案 1 :(得分:2)
我注意到四个主要问题:
您正在混合scanf
和gets
。 gets
尤其是一个坑,因为它会超越缓冲而没有悔恨。一般来说,最好选择其中一种。我发现最可靠的是使用fgets
将每一行扫描到一个静态缓冲区中,并从那里进行解析。
您正在呼叫fflush(stdin)
。虽然流刷新是为输出流定义的,但它不是用于输入;可能这条线没有按照你的预期行事。
您正在使用c ++编译器编译c。因此,您创建的代码并不适合任何地方。具体来说,您将struct Stock
缩短为Stock
,但永远不会typedef struct Stock Stock;
您还使用C ++和C99样式for
循环,这不是太大的事,但是{{1} (linux编译器)会抱怨,直到你给它提供一些标志。
政变!您目前正在读取和编写二进制文件,如下所示:
gcc
问题是数组作为参数传递给函数被视为指针,而不是数组。因此, fwrite(&s, sizeof(s[10]), n, f);
fread(&s, sizeof(s[10]), n, f);
为您提供了指针的位置,而不是数组的地址。 &s
然后用二进制文件的内容粉碎你的堆栈。将这些行更改为
fread
解决了主要问题。
使用数组时,你很少发现自己的地址,因为他们衰减指向他们的第一个元素的指针。
答案 2 :(得分:1)
您正在尝试访问空指针。因此它会抛出此错误。
fprintf(f,"%s\n",s[i].name);
在这些行中,对于程序中的某些情况,s [i]可能为null,因此您尝试访问对象的成员变量,该变量为NULL。
所以只有它会抛出这个运行时错误。