I cut the main out when posting on this forum to make the code shorter.
Here is the total code
Code:
#include <stdio.h>
#include <string.h>
int menu(void);
void cost(void);
void distance(void);
void showchart(void);
float price;
int option, x, y, dist;
int grid[10][10];
char names[10][5];
main()
{
int choice;
choice = menu();
while (choice<0 || choice >3)
{
choice = menu();
}
while (choice !=0)
{
switch (choice)
{
case 1: distance(); break;
case 2: cost(); break;
case 3: showchart(); break;
}
choice = menu();
}
printf("\n***Thank you for using this programme!***\n");
}
int menu()
{
int opt;
printf("\n\n\nDo you want to\n");
printf("<1>Calculate the distance between X and Y\n");
printf("<2>Calculate the cost between X and Y\n");
printf("<3>Display the chart\n");
printf("<0>Quit\n\n\n");
printf("Please enter your choice: ");
scanf("%d", &opt);
printf("\n");
return (opt);
}
void cost()
{
printf("\nLocation 1: ");
scanf("%d", &x);
while (x < 1 || x >10)
{
printf("\nPlease enter a valid Location 1: ");
scanf("%d", &x);
}
printf("Location 2: ");
scanf("%d", &y);
while (y < 1 || y >10)
{
printf("\nPlease enter a valid Location 2: ");
scanf("%d", &y);
}
price = grid[x][y] * 0.4;
printf ("\n");
printf ("The price for your journey is: œ");
printf ("%6.2f", price);
printf ("\n");
}
void distance ()
{
printf("\nLocation 1: ");
scanf("%d", &x);
while (x < 1 || x >10)
{
printf("\nPlease enter a valid Location 1: ");
scanf("%d", &x);
}
printf("Location 2: ");
scanf("%d", &y);
while (y < 1 || y >10)
{
printf("\nPlease enter a valid Location 2: ");
scanf("%d", &y);
}
dist = grid[x][y];
printf ("\n");
printf ("The distance for your journey is: ");
printf ("%d", dist);
printf (" miles\n");
}
void showchart ()
{
int x, row, col;
FILE *in_file;
in_file = fopen("places.txt","r");
for (x=0;x<=9; x++)
{
fscanf(in_file, "%4c", names[x]);
}
printf("\n ");
for (x=0; x<=9; x++)
{
printf("%6s",names[x]);
}
printf("\n");
for (row=0; row<=9; row++)
{
printf("%s", names[row]);
for(col=0; col<=9; col++)
{
fscanf(in_file, "%d", &grid[row][col]);
printf("%6d", grid[row][col]);
}
printf("\n");
}
fclose(in_file);
}