i need the program to read from a file and write to a file.. everything was working perfectly until i tried to add a menu to it. Here's my code. Any help is appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct car
{
int year;
char number[9];
char colour[10];
float engine;
int tax;
} car_type;
int main()
{
int i,j,x;
void print_file ();
void print_screen ();
car_type cars[6];
FILE* fp;
FILE* fp2;
fp = fopen("indata.txt","r");
fp2 = fopen("car_tax.txt","w");
i = 0;
while(!feof(fp))
{
fscanf (fp, "%d", &cars[i].year);
fscanf (fp, "%s", &cars[i].number);
fscanf (fp, "%s", &cars[i].colour);
fscanf (fp, "%f", &cars[i].engine);
i++;
}
for (j=0;j<=i;j++)
{
if (cars[j].year <=5 && cars[j].engine <=1.6)
cars[j].tax = 150;
if (cars[j].year <=5 && cars[j].engine >1.6)
cars[j].tax = 300;
if (cars[j].year >5 && cars[j].engine < 1.4)
cars[j].tax = 200;
if (cars[j].year >5 && cars[j].engine >= 1.6)
cars[j].tax = 600;
if (cars[j].year > 5)
{
if (cars[j].engine >= 1.4 && cars[j].engine < 1.6)
cars[j].tax = 400;
}
}
printf ("Welcome to the program\n");
printf ("\nPlease enter a choice");
printf ("\n\tMENU\n\n");
printf ("1.\tPrint on screen\n");
printf ("2.\tPrint data to file\n");
printf ("3.\tEnd program\n");
switch (x)
{
case '1':
print_file (j, cars[].year, cars[].number, cars[].colour, cars[].engine, cars[].tax);
break;
case '2':
print_screen (j,cars[].tax);
break;
case '3':
printf ("\nThank you for using the programme goodbye\n");
break;
default:
printf ("Error! incorrect choice please enter another choice");
break;
}
fflush (fp);
fclose (fp);
fflush (fp2);
fclose (fp2);
return (EXIT_SUCCESS);
}
void print_screen (int j, int cars[].tax)
{
int a;
for (a=0;a<=j;a++)
{
printf ("\nTax on car %d = Euro %d\n", a, cars[j].tax);
}
}
void print_file (int j, int cars[].year, ch cars[].number, ch cars[].colour, float cars[].engine, int cars[].tax)
{
int b;
for (b=0;b<j;b++)
{
fprintf(fp2,"%d\t",cars[b].year);
fprintf(fp2,"%s\t",cars[b].number);
fprintf(fp2,"%s\t",cars[b].colour);
fprintf(fp2,"%f\t",cars[b].engine);
fprintf(fp2,"%d\n",cars[b].tax);
}
}