I have wrote this program to do i of 6 thing to
1) Initialise: Read static data from a file and
a) store in appropriate data structures at run time
b) sort into alphabetic order for subsequent display in the grid.
2) Menu: A menu to support the following:
Have to use a switch statement for menu and also validation for menu.
My program should display an error message when the user input an city that it not specified within the array or my file. Also it should loop if they tpe in a mber or a incoorect place for the destination.
a) Display Chart: Display the grid like the one in a code but trying to open a file with the towns in and there distance.
b) CalcDistance: Allow input of a Journey between two points via up to 10intermediate stops.
c) Totalcost: Allow input of two points and calculate the cost of the journey at 40ppm up to 100 miles (or km) and 30ppm for miles (or km) in excess of this.
d) Exit: to exit the program.
3) PlaceSearch: search for valid places during input of route details
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
struct Chart
{
char Town[10][10];
int Distance[10][10];
};
Chart chart=
{
{ "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truro", "York" },
{ 0, 23, 12, 89, 456, 123, 46, 732, 345, 123 },
{ 23, 0, 46, 234, 123, 46, 89, 234, 567, 90 },
{ 12, 46, 0, 767, 456, 46, 234, 123, 732, 35 },
{ 89, 234, 767, 0, 732, 32, 48, 67, 98, 100 },
{ 456, 123, 456, 732, 0, 234, 46, 89, 89, 732 },
{ 123, 46, 46, 32, 234, 0, 123, 46, 123, 234 },
{ 46, 89, 234, 48, 46, 123, 0, 46, 89, 19 },
{ 732, 234, 123, 67, 89, 46, 46, 0, 123, 732 },
{ 345, 567, 732, 98, 89, 123, 89, 123, 0, 78 },
{ 123, 90, 35, 100, 732, 234, 19, 732, 78, 0 },
};
int ReadChart(const char Towns[],const Chart* chart )
{
FILE *fp;
fp = fopen("Towns.txt", "r");
if (fp == NULL)
return 0;
else
{
size_t res = fread(chart, sizeof(Chart), 1, fp);
fclose(fp);
return res;
}
}
int WriteChart(const char Towns[], const Chart* chart)
{
FILE *fp;
fp = fopen("Towns.txt", "w");
if (fp == NULL)
return 0;
else
{
size_t res = fwrite(chart, sizeof(Chart), 1, fp);
fclose(fp);
return res;
}
}
int main(void)
{
char Distance;
int valid_input;
int row,col;
char des1,des2,des3;
int menu; /* Holds all chices open to the user */
double total = 0.00; /* the result of the calculation */
printf("\n\nWhat would you like to do?\n\n"); /* WRITE instructions */
printf("\t1 = Display Mileage chart from text file\n");
printf("\t2 = Input of a Journey between two points via up to 3 intermediate stops\n");
printf("\t3 = Display total cost of journey at 40 ppm up to 100 miles and 30ppm for mileas in excess of this\n");
printf("\t4 = Exit program\n");
printf("\n\nPleas make your selection now:\n\t");
scanf("%d",&menu); /* READ calculation type */
valid_input = 0;
while( valid_input == 0 )
{
printf("\t Please enter first destination point\n");
printf("%s",des1);
printf("\t Please enter second destination point\n");
printtf("%s",des2);
printf("\t Please enter thrid destination point\n");
printf("%s",des3);
else
{
printf("\tError: Invalid \n");
}
}
switch (menu != 4)
{
case 1:
FILE *infile;
infile = fopen("Towns.dat", "r"); // using relative path name of file
if (infile == NULL)
{
printf("Unable to open file.");
}
for(row=1; row<11;row++) // reads in the data into the array in 2 loops
{
for(col=1;col<11;col++)
{
fscanf(infile,"%d", chart[row-1][col-1]);
}
}
for(row=0; row<10;row++)
{
printf("\n\n");
for(col=0;col<10;col++)
{
printf("%-2d ",chart[row][col]);
}
}
printf("\n\n continue?");
scanf("%d", &menu);
break;
case 2:
prinf("\t Please enter first destination point\n");
printf("%s",des1);
prinf("\t Please enter second destination point\n");
printf("%s",des2);
prinf("\t Please enter thrid destination point\n");
printf("%s",des3);
break;
case 3:
if (Distance => 100)
{
total= 0.40*100;
}
if (Distance < 100)
{
total= 0.30*100;
}
break;
case 4:
printf("Thank you for using my program");
break;
default:
printf("Invalid option selected\n");
}
int search (node *root, int key)
{
if ( root != NULL)
{
if ( key == root->data )
return 1;
else if ( key < root->data )
return search ( root->left, key );
else if ( key > root->data )
return search ( root->right, key );
}
return 0;
}
}
I use Miracle C to compile my program and unix.
I am sorry for pesting you with this huge problem but i have tried other methords but failed and i was wondering if you could help me out please thank you