Code:
// Cproject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
//Function Prototypes
int loc[10][10];
void distances(int[10][10],char [10][11]);
char towns[10][11];
void townSearch(char[10][11]);
void displayGrid();
void cost(int [10][10],char [10][11]);
int initialise();
char menu();
char option;
int match;
int arrayCount;
char input,rtn;
int arrayLocs[5];
int distance;
int totalCost;
int longDist;
/************************************************************************/
int main ()
{
if (initialise()==0)
return 1;
do
{
system("cls");
fflush(stdin);
option = menu();
switch(option)
{
case 'a':
displayGrid();
system("pause");
break;
case 'b':
distances(loc,towns);
system("pause");
break;
case 'c':
cost(loc,towns);
system ("pause");
break;
case 'd':
townSearch(towns);
system ("pause");
break;
case 'e':
return 1;
default:
printf("Wrong input - Please Try Again\n\n");
system ("pause");
}
}while (option !='e');
return 1;
}
/************************************************************************/
int initialise()
{
FILE *fp; /*creates a file pointer named fp*/
int x,y, rowno=0;
if ((fp=fopen("C:\\Mileages.dat", "r")) == NULL)
{
printf("\nFile could not be opened: Program terminating\n\n");
system("pause");
return 0;
}
else
{
for (x = 0; x < 10; x++)
for (y = 0; y < 10; y++)
fscanf(fp, "%d", &loc[x][y]);
for (x = 0; x < 10; x++)
fscanf(fp, "%s", towns[x]);
}
fclose (fp);
return 1;
}
char menu()
{
/* Menu Function */
char choice, rtn;
do
{
system("cls");
printf("What would you like to do ?\n\n");
printf(" a: Display mileage table\n\n");
printf(" b: Calculate distances between points\n\n");
printf(" c: Calculate cost of journey\n\n");
printf(" d: Search for a town in the list\n\n");
printf(" e: Exit\n\n");
scanf("%c%c", &choice, &rtn);
if (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'e')
{
printf("\n\nWrong input, please try again\n\n");
system("pause");
}
}while (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'e');
return choice;
}
void displayGrid()
{
system("cls");
int x,y,rowno=0;
printf(" ");
for (x = 1; x < 11*10+3; x++)
printf("_");
printf("\n | |");
for (x=0;x<10;x++)
printf("%10s", towns[x]);
printf(" |\n ");
printf(" ");
for (x = 1; x < 11*10+3; x++)
printf("_");
printf("\n");
for (x=0;x<10;x++)
{
printf(" |%-10s|",towns[x]);
for(y = 0;y < 10;y++)
{
printf("%10d", loc[x][y]);
if(y!=0 && y % 9 == 0)
{
printf(" |\n");
}
}
}
// Base Line
printf(" ");
for (x = 1; x < 11*10+3; x++)
printf("_");
printf("\n\n\n");
}
void distances(int loc[10][10],char towns[10][11])
{
system("cls");
for (int x=0;x<10;x++)
printf("%10s\n", towns[x]);
match=0;
do
{
printf("Please enter the starting point for your journey from the above list\t");
scanf("%s%c", &input,&rtn);
for (int i = 0; i<10; i++)
{
if(strcmp(&input,towns[i]) ==0)
{
arrayLocs[0] = i; // Put start location as first element of array
match = 1;
}
}
}while(match != 1);
match=0;
do
{
printf("Please enter the destination point for your journey from the above list\t");
scanf("%s%c", &input,&rtn);
for (int i = 0; i<10; i++)
{
if(strcmp(&input,towns[i]) ==0)
{
arrayLocs[4] = i; // Put final destination as last element in array
match = 1;
}
}
}while(match != 1);
printf("The direct distance between your chosen towns is %d", loc[arrayLocs[0]][arrayLocs[4]]);
printf("\tmiles\n\n");
printf(" Please enter any intermediate points along your journey - up to a maximum of three points\n");
printf(" Or type ""q"" to calculate the journey distance\n\n");
distance=0;
arrayCount=0;
int j;
j=0;
match=0;
while(j<3 && input !='q') // Loop three times or until q is entered
{
scanf("%s%c", &input,&rtn);
for (int i = 0; i<10; i++) //Iterate through list of towns
{
if(strcmp(&input,towns[i]) ==0)
{
arrayLocs[j+1] = i; // Start location is at arrayLocs[0], so next available space is arrayLocs[1]
distance=distance+loc[arrayLocs[j]][arrayLocs[j+1]];
arrayCount=j+1;
match=1; //Set flag to 1 to show the if statement has been entered
}
}
if (match==0 && input !='q') // If town entered is not in list, show error message
{
printf("You may have miss-typed the location name. Please try again\n");
printf("or type ""q"" to finish\n\n");
j--; // Reset loop counter
}
else
{
match=0; // Reset flag for next loop
}j++;
}
distance=distance+loc[arrayLocs[arrayCount]][arrayLocs[4]];
printf("The distance of your journey is now %d",distance);
printf("\tmiles\n\n");
}
void townSearch(char towns[10][11])
{
match=0;
// input of search for a place
printf("\nPlease enter a town to test if it is in the list\t ");
scanf("%s%c", &input,&rtn);
for (int i = 0; i<10; i++)
{
if(strcmp(&input,towns[i]) ==0)
match = 1;
}
if(match==1)
{
printf("Your entry is in the list\n\n");
}
else
{
printf("Your selection is not in the list of towns\n\n");
}
}
void cost(int loc[10][10],char towns[10][11])
{
system("cls");
for (int x=0;x<10;x++)
printf("%10s\n", towns[x]);
distance=0;
match=0;
double totalCost;
totalCost=0;
double longDist;
longDist=0;
do
{
printf("Please enter the starting point for your journey from the above list\t");
scanf("%s%c", &input,&rtn);
for (int i = 0; i<10; i++)
{
if(strcmp(&input,towns[i]) ==0)
{
arrayLocs[0] = i; // Put start location as first element of array
match = 1;
}
}
}while(match != 1);
match=0;
do
{
printf("Please enter the destination point for your journey from the above list\t");
scanf("%s%c", &input,&rtn);
for (int i = 0; i<10; i++)
{
if(strcmp(&input,towns[i]) ==0)
{
arrayLocs[4] = i; // Put final destination as last element in array
match = 1;
}
}
}while(match != 1);
printf("The direct distance between your chosen towns is %d", loc[arrayLocs[0]][arrayLocs[4]]);
printf("\tmiles\n\n");
distance=loc[arrayLocs[0]][arrayLocs[4]];
//distance=50;
if(distance <= 100)
{
totalCost = (distance*0.40);
printf("The cost is %.2f ", totalCost);
printf(" pounds\n\n");
}
else
{
longDist = distance - 100;
longDist =(longDist * 0.30);
totalCost = (100 * 0.40) + longDist;
printf("The total cost of the journey is %.2f ", totalCost);
printf(" pounds\n\n");
}
}
Anyone can show me would be much appreciated.