I have developed a little program which reads in data from a file, does some specific computation and prints the results to the screen and another file. This program works fine whilst doing this but when I try to add a user menu using 'switch' I run into problems. The code with the user menu compiles OK but when I run it, it crashes or doesn't work correctly, depending which option you take. So my question is: can you please help identify where I have gone wrong? I am relatively new at this and don't know a lot about the 'switch' command.
I know I should use functions, that's next! I'll include both sets of code:
Program 1 - Without Menu
#include <stdlib.h>
#include <stdio.h>
// define structure
typedef struct car
 {
    char brand[20];
    char model[20];
    int engine;
    float price;
    int year;
 } car_type;
int main(void)
{
 car_type car[20];
 int year, i;
 char brand[20], model[20], space;
 int engine;
 float price, new_price;
 i = 0;
 // read from file
 FILE* fp;
 fp = fopen("indata.txt", "r");
 FILE* fp_out;
 fp_out = fopen("outdata.txt", "w");
 if (fp == NULL)
    {
        printf("Opening of file failed\n");
        return 0;
    }
 else
    {
        printf("Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n\n");
        fprintf(fp_out, "Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n");
        while (!feof(fp))
            {
                fscanf(fp, "%s%c%s%c%d%c%f%c%d\n", &car[i].brand, &space, &car[i].model, &space, &car[i].engine, &space, &car[i].price, &space, &car[i].year);
                //display info
                printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
                fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
                // Compute new price
                    if (car[i].year > 0)
                        {
                            printf("N/A\n");
                            fprintf(fp_out, "N/A\n");
                        }
                    else
                        {
                            if (car[i].engine >= 1600 && car[i].price>= 20000)
                                {
                                    new_price = (car[i].price)*(0.85);
                                    printf(" %1.1f euro \n", new_price);
                                    fprintf(fp_out," %1.1f euro \n", new_price);
                                }
                            else
                                {
                                    if (car[i].engine>=1600)
                                        {
                                            new_price = (car[i].price)*(0.95);
                                            printf(" %1.1f euro \n", new_price);
                                            fprintf(fp_out," %1.1f euro \n", new_price);
                                        }
                                    else
                                        {
                                            if (car[i].price >= 20000)
                                                {
                                                    new_price = (car[i].price)*(0.9);
                                                    printf(" %1.1f euro \n", new_price);
                                                    fprintf(fp_out," %1.1f euro\n", new_price);
                                                }
                                            else
                                                {
                                                    printf("N/A \n");
                                                    fprintf(fp_out, "N/A\n");
                                                }
                                        }
                                }
                        }
            i++;
            }
    }
 return(EXIT_SUCCESS);
}
Program 2 - With Menu
#include <stdlib.h>
#include <stdio.h>
// define structure
typedef struct car
 {
    char brand[20];
    char model[20];
    int engine;
    float price;
    int year;
 } car_type;
int main(void)
{
 car_type car[20];
 int year, i, option;
 char brand[20], model[20], space;
 int engine;
 float price, new_price;
 i = 0;
 // read from file
 FILE* fp;
 fp = fopen("indata.txt", "r");
 FILE* fp_out;
 fp_out = fopen("outdata.txt", "w");
 //user menu
 option = 0;
 while (option != 4)
 {
     printf("To have output printed on to screen only - select option 1 \n");
     printf("To have output printed to file only - select option 2 \n");
     printf("to have output printed to both - select option 3 \n");
     printf("To exit - press 4 \ncc32");
     scanf("%d", &option);
     switch (option)
        {
            //Case 1
            case 1:
            {
                if (fp == NULL)
                    {
                        printf("Opening of file failed\n");
                        return 0;
                    }
                else
                    {
                        printf("Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n\n");
                        while (!feof(fp))
                            {
                                //display info
                                printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
                                // Compute new price
                                if (car[i].year > 0)
                                    {
                                        printf("N/A\n");
                                    }
                                else
                                    {
                                        if (car[i].engine >= 1600 && car[i].price>= 20000)
                                            {
                                                new_price = (car[i].price)*(0.85);
                                                printf(" %1.1f euro \n", new_price);
                                            }
                                        else
                                            {
                                                if (car[i].engine>=1600)
                                                    {
                                                        new_price = (car[i].price)*(0.95);
                                                        printf(" %1.1f euro \n", new_price);
                                                    }
                                                else
                                                    {
                                                        if (car[i].price >= 20000)
                                                            {
                                                                new_price = (car[i].price)*(0.9);
                                                                printf(" %1.1f euro \n", new_price);
                                                            }
                                                        else
                                                            {
                                                                    printf("N/A \n");
                                                            }
                                                    }
                                            }
                                    }
                            i++;
                            }
                    }
            break;
            }
            //Case 2
            case 2:
                {
                    if (fp == NULL)
                        {
                            printf("Opening of file failed\n");
                            return 0;
                        }
                    else
                        {
                            fprintf(fp_out, "Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n");
                            while (!feof(fp))
                                {
                                    fscanf(fp, "%s%c%s%c%d%c%f%c%d\n", &car[i].brand, &space, &car[i].model, &space, &car[i].engine, &space, &car[i].price, &space, &car[i].year);
                                    //display info
                                    fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
                                    // Compute new price
                                    if (car[i].year > 0)
                                        {
                                            fprintf(fp_out, "N/A\n");
                                        }
                                    else
                                        {
                                            if (car[i].engine >= 1600 && car[i].price>= 20000)
                                                {
                                                    new_price = (car[i].price)*(0.85);
                                                    fprintf(fp_out," %1.1f euro \n", new_price);
                                                }
                                            else
                                                {
                                                    if (car[i].engine>=1600)
                                                        {
                                                            new_price = (car[i].price)*(0.95);
                                                            fprintf(fp_out," %1.1f euro \n", new_price);
                                                        }
                                                    else
                                                        {
                                                            if (car[i].price >= 20000)
                                                                {
                                                                    new_price = (car[i].price)*(0.9);
                                                                    fprintf(fp_out," %1.1f euro\n", new_price);
                                                                }
                                                            else
                                                                {
                                                                    fprintf(fp_out, "N/A\n");
                                                                }
                                                        }
                                                }
                                        }
                                i++;
                                }
                        }
                    break;
                }
            //Case 3
            case 3:
            {
                if (fp == NULL)
                    {
                        printf("Opening of file failed\n");
                        return 0;
                    }
                else
                    {
                        printf("Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n\n");
                        fprintf(fp_out, "Brand \t Model \t Engine \t Price \t Old(0)/New(1) \t New Price\n");
                        while (!feof(fp))
                            {
                                fscanf(fp, "%s%c%s%c%d%c%f%c%d\n", &car[i].brand, &space, &car[i].model, &space, &car[i].engine, &space, &car[i].price, &space, &car[i].year);
                                //display info
                                printf("%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
                                fprintf(fp_out, "%7s\t %6s\t %4d cc\t %1.1f euro\t %d\t ", car[i].brand, car[i].model, car[i].engine, car[i].price, car[i].year);
                                // Compute new price
                                    if (car[i].year > 0)
                                        {
                                            printf("N/A\n");
                                            fprintf(fp_out, "N/A\n");
                                        }
                                    else
                                        {
                                            if (car[i].engine >= 1600 && car[i].price>= 20000)
                                                {
                                                    new_price = (car[i].price)*(0.85);
                                                    printf(" %1.1f euro \n", new_price);
                                                    fprintf(fp_out," %1.1f euro \n", new_price);
                                                }
                                            else
                                                {
                                                    if (car[i].engine>=1600)
                                                        {
                                                            new_price = (car[i].price)*(0.95);
                                                            printf(" %1.1f euro \n", new_price);
                                                            fprintf(fp_out," %1.1f euro \n", new_price);
                                                        }
                                                    else
                                                        {
                                                            if (car[i].price >= 20000)
                                                                {
                                                                    new_price = (car[i].price)*(0.9);
                                                                    printf(" %1.1f euro \n", new_price);
                                                                    fprintf(fp_out," %1.1f euro\n", new_price);
                                                                }
                                                            else
                                                                {
                                                                    printf("N/A \n");
                                                                    fprintf(fp_out, "N/A\n");
                                                                }
                                                        }
                                                }
                                        }
                            i++;
                            }
                    }
            break;
            }
        }
 }
 return(EXIT_SUCCESS);
}
 
     
    