Ok, today is the final and I'm still confused with the struct.
Last assignment was supposed to be transformaed from regular arrays to struct and even though I did it and it works, it's more like each member inside of a record is a array rather than array of records. Can someone please help me out to fix it (this is not for a grade anymore, I just want to understand it before the final). Thanks in advance. This is what I got
#include <stdio.h>
#include <stdlib.h>
/********************************* Constants **********************************/
#define MAX 5 //max elements in an array
#define SMAX 10 //max chars for stock ticker
/**************************** Function Prototypes *****************************/
void Validate_File_Open(FILE *report);
void Banner(void);
void Input_Ticker(char *stock_ticker_p);
void Input_Price_Per_Share(int *whole_p, int *numerator_p, int *denominator_p);
float Aproach_Rule(int whole, int numerator, int denominator);
void Input_Num_Shares(int *num_shares_p);
float Calculate_Stock_Value(int num_shares, float price_share);
void Insert_Blank_Lines(FILE *report);
void Input_Format_Style(int *choice_p);
void Print_Details(FILE *report, int num_shares[MAX], char stock_ticker[MAX][SMAX],
float price_share[MAX], float stock_value[MAX], int i);
void Print_Details2(FILE *report, int num_shares[MAX], char stock_ticker[MAX][SMAX],
float price_share[MAX], float stock_value[MAX], int i);
void Report_And_Exit(void);
void Flush(void);
int main(void)
{
/********************************* Variables **********************************/
struct record {int num_shares[MAX]; char stock_ticker[MAX][SMAX];
float price_share[MAX]; float stock_value[MAX];} record;
int choice, whole, numerator, denominator, i;
/**************************** Input / Processing ******************************/
FILE *report;
report = fopen("Report.txt", "w");
Validate_File_Open(report);
for ( i =0; i < MAX; i++)
{
Input_Ticker(record.stock_ticker[i]);
Input_Price_Per_Share(&whole, &numerator, &denominator);
record.price_share[i] = Aproach_Rule(whole, numerator, denominator);
Input_Num_Shares(&record.num_shares[i]);
record.stock_value[i] = Calculate_Stock_Value(record.num_shares[i], record.price_share[i]);
}
/*********************************** Output ***********************************/
Flush();
Insert_Blank_Lines(report);
Input_Format_Style(&choice);
if (choice == 0)
{
Print_Details(report, record.num_shares, record.stock_ticker, record.price_share, record.stock_value, i);
}
else if (choice == 1)
{
Print_Details2(report, record.num_shares, record.stock_ticker, record.price_share, record.stock_value, i);
}
else
{
Report_And_Exit();
}
fclose(report);
Flush();
printf("\n\n Press any key to terminate . . . \n");
getchar();
return 0;
}
/************************** Function Definition #x *****************************
void Validate_File_Open(FILE *report)
{
if ((report = fopen("Report.txt", "w+")) == NULL)
{
printf("\n Cannot open Report.txt - The Program will now exit\n\n");
getchar();
exit(1);
}
}
/************************** Function Definition #x *****************************
void Input_Ticker(char *stock_ticker_p)
{
printf("\nEnter stock's ticker symbol: ");
scanf("%s",stock_ticker_p);
}
/************************** Function Definition #x *****************************
void Input_Price_Per_Share(int *whole_p, int *numerator_p, int *denominator_p)
{
printf("Enter current price per share: ");
scanf("%d %d/%d",whole_p,numerator_p,denominator_p);
}
/************************** Function Definition #x *****************************
float Aproach_Rule(int whole, int numerator, int denominator)
{
if (denominator == 0)
{
return (whole + (float)numerator / 1);
}
else
{
return (whole + (float)numerator/(float)denominator);
}
}
/************************** Function Definition #x *****************************
void Input_Num_Shares(int *num_shares_p)
{
printf("Enter number of shares purchased: ");
scanf("%d",num_shares_p);
}
/************************** Function Definition #x *****************************
float Calculate_Stock_Value(int num_shares, float price_share)
{
return num_shares * price_share;
}
/************************** Function Definition #x *****************************
void Insert_Blank_Lines(FILE *report)
{
fprintf(report, "\n\n\n\n\n\n\n\n\n\n");
}
/************************** Function Definition #x *****************************
void Input_Format_Style(int *choice_p)
{
printf("In which format would you like to print report?\n");
printf(" (Press '0' for old style and '1' for modern style(recommended): ");
scanf("%d", choice_p);
}
/************************** Function Definition #x *****************************
void Print_Details(FILE *report, int num_shares[MAX], char stock_ticker[MAX][SMAX],
float price_share[MAX], float stock_value[MAX], int i)
{
for ( i =0; i < MAX; i++)
{
fprintf(report, "%d shares of %s @ %.2f per share equals $%.2f\n",
num_shares[i], stock_ticker[i], price_share[i], stock_value[i]);
}
}
/************************** Function Definition #x *****************************
void Print_Details2(FILE *report, int num_shares[MAX], char stock_ticker[MAX][SMAX],
float price_share[MAX], float stock_value[MAX], int i)
{
fprintf(report,
"\n Number of Shares Ticker Symbol Price Per Share");
fprintf(report, " Stock Value");
fprintf(report,
"\n -----------------*-----------------*-------------------*");
fprintf(report, "-------------\n");
for ( i =0; i < MAX; i++)
{
fprintf(report, " ");
fprintf(report, " %-5.0d %-5s%20.2f%20.2f\n",
num_shares[i], stock_ticker[i], price_share[i], stock_value[i]);
}
fprintf(report,
" --------------------------------------------------------");
fprintf(report, "-------------");
}
/************************** Function Definition #x *****************************
void Report_And_Exit(void)
{
printf("invalid input, the program will now exit\n");
getchar();
exit(1);
}
/************************** Function Definition #x *****************************
void Flush(void)
{
int ch;
do
{
ch = getchar();
}
while (ch != EOF && ch != '\n');
clearerr(stdin);
}