Also anyone know how to tidy the following code up to make it look better, maybe put it under one function etc..... it works as it is but looks a bit untidy.
The code basically allows the user to input either 0, 1, 2 or 3 stops before the destination.
I have declared the variables in the top part of the code.
Code:
void distance ()
{
int choice;
choice = calcdis();
while (choice<0 || choice >4)
{
choice = calcdis();
}
while (choice !=0)
{
switch (choice)
{
case 1: dis0(); break;
case 2: dis1(); break;
case 3: dis2(); break;
case 4: dis3(); break;
case 5: menu(); break;
}
choice = calcdis();
}
printf("\n***Thank you for using this programme!***\n");
}
int calcdis ()
{
int option;
printf("\n\n\nHow many stops do you want?\n");
printf("<1>No Stops\n");
printf("<2>1 Stops\n");
printf("<3>2 Stops\n");
printf("<4>3 Stops\n");
printf("<0> Main Menu\n\n\n");
printf("Please enter your choice: ");
scanf("%d", &option);
printf("\n");
return (option);
}
void dis0()
{
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);
}
x = x-1;
y = y-1;
dist = grid[x][y];
printf ("\n");
printf ("The distance for your journey is: ");
printf ("%d", dist);
printf (" miles\n");
}
void dis1()
{
printf("\nLocation 1: ");
scanf("%d", &x);
while (x < 1 || x >10)
{
printf("\nPlease enter a valid Location 1: ");
scanf("%d", &x);
}
printf("Stop 1: ");
scanf("%d", &y);
while (y < 1 || y >10)
{
printf("\nPlease enter Stop 1: ");
scanf("%d", &y);
}
printf("Location 2: ");
scanf("%d", &z);
while (z < 1 || z >10)
{
printf("\nPlease enter a valid Location 2: ");
scanf("%d", &z);
}
x = x-1;
y = y-1;
z = z-1;
dist1 = grid[x][y];
dist2 = grid[y][z];
dist = dist1 + dist2;
printf ("\n");
printf ("The distance for your journey is: ");
printf ("%d", dist);
printf (" miles\n");
}
void dis2()
{
printf("\nLocation 1: ");
scanf("%d", &x);
while (x < 1 || x >10)
{
printf("\nPlease enter a valid Location 1: ");
scanf("%d", &x);
}
printf("Stop 1: ");
scanf("%d", &y);
while (y < 1 || y >10)
{
printf("\nPlease enter Stop 1: ");
scanf("%d", &y);
}
printf("Stop 2: ");
scanf("%d", &z);
while (z < 1 || z >10)
{
printf("\nPlease enter Stop 2: ");
scanf("%d", &z);
}
printf("Location 2: ");
scanf("%d", &xz);
while (xz < 1 || xz >10)
{
printf("\nPlease enter a valid Location 2: ");
scanf("%d", &xz);
}
x = x-1;
y = y-1;
z = z-1;
xz = xz-1;
dist1 = grid[x][y];
dist2 = grid[y][z];
dist3 = grid[z][xz];
dist = dist1 + dist2 + dist3;
printf ("\n");
printf ("The distance for your journey is: ");
printf ("%d", dist);
printf (" miles\n");
}
void dis3()
{
printf("\nLocation 1: ");
scanf("%d", &x);
while (x < 1 || x >10)
{
printf("\nPlease enter a valid Location 1: ");
scanf("%d", &x);
}
printf("Stop 1: ");
scanf("%d", &y);
while (y < 1 || y >10)
{
printf("\nPlease enter Stop 1: ");
scanf("%d", &y);
}
printf("Stop 2: ");
scanf("%d", &z);
while (z < 1 || z >10)
{
printf("\nPlease enter Stop 2: ");
scanf("%d", &z);
}
printf("Stop 3: ");
scanf("%d", &xz);
while (xz < 1 || xz >10)
{
printf("\nPlease enter Stop 3: ");
scanf("%d", &xz);
}
printf("Location 2: ");
scanf("%d", &xy);
while (xy < 1 || xy >10)
{
printf("\nPlease enter a valid Location 2: ");
scanf("%d", &xy);
}
x = x-1;
y = y-1;
z = z-1;
xz = xz-1;
xy = xy-1;
dist0 = grid[x][y];
dist1 = grid[y][z];
dist2 = grid[z][xz];
dist3 = grid[xz][xy];
dist = dist0 + dist1 + dist2 + dist3;
printf ("\n");
printf ("The distance for your journey is: ");
printf ("%d", dist);
printf (" miles\n");
}