If someone could just please look through the code and see why the double(aka pr
ID: 3625887 • Letter: I
Question
If someone could just please look through the code and see why the double(aka price) is not being saved and instead being said to be the default null value of double it would be greatly appreciated.#include <stdio.h>
struct Stock
{
int number;
double price;
};
int main(void)
{
double ave = 0.0;
int max=0;
int min =0;
struct Stock *st;
int numLine;
printf("Enter the number of Stocks you would like to store:");
scanf("%d",&numLine);
int i=0;
double temp;
st=malloc(numLine*sizeof(struct Stock));
for(i=0;i<numLine;i++)
{
printf(" Enter stock number for stock #%d : ",i+1);
scanf("%d",&st[i].number);
printf(" Enter the stock price: ");
scanf("%f",&temp);
st[i].price=temp;
if(st[i].price>st[max].price)
max=i;
if(st[i].price<st[min].price)
min=i;
ave+=st[i].price;
}
ave/=(numLine*1.0);
for(i=0;i<numLine;i++)
{
printf(" Stock Number: %d", st[i].number);
printf(" Stock Price: %.2f", st[i].price);
}
printf(" The highest stock is %d with a value of %.2f.",st[max].number,st[max].price);
printf(" The lowest stock is %d with a value of %.2f.",st[min].number,st[min].price);
printf(" The average stock price is %.2f.",ave);
printf(" Writing to binary file..... Reading from binary file....");
FILE *fout;
fout=fopen("stock","w");
for(i=0;i<numLine;i++)
{
fprintf(fout,"%d %f ",st[i].number,st[i].price);
}
fclose(fout);
FILE *fin;
fin=fopen("stock","r");
if(fin==NULL)
{
printf("File Not Found");
exit(2);
}
int tempNumber;
double tempDouble;
while(!feof(fin))
{
fscanf(fin,"%d",&tempNumber);
fscanf(fin,"%f",&tempDouble);
printf(" %d %f",tempNumber,tempDouble);
}
fclose(fin);
}
Explanation / Answer
Well i figured this out, so never mind, turns out when doing scan f i had to use %lf instead of just %f
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.