Having problem with C program plz help

• 04-03-2008
Game
Having problem with C program plz help
You should replace the distances input function with a function to read from a text file containing a list of 10 short place names followed by the requisite number of distances

London Bath Cardiff Carlisle Durham Exeter Leeds Norwich Truro York
London - 23 12 89 456 123 46 732 345 123
Bath 23 - 46 234 123 46 89 234 567 90
Cardiff 12 46 - 767 456 46 234 123 732 35
Carlisle 89 234 767 - 732 32 48 67 98 100
Durham 456 123 456 732 - 234 46 89 89 732
Exeter 123 46 46 32 234 - 123 46 123 234
Leeds 46 89 234 48 46 123 - 46 89 19
Norwich 732 234 123 67 89 46 46 - 123 732
Truro 345 567 732 98 89 123 89 123 - 78
York 123 90 35 100 732 234 19 732 78 -

Fig 2 – Example Data (not accurate)

The details above are for example only and are not accurate distances. You should use different towns/cities and include valid values.

The menu should enable the grid to be displayed and also allow for journeys via intermediate stops to be input thus enabling a route to be calculated, for example, from London to Durham via Norwich and Leeds. In addition it is proposed that two rates of travel be catered for - journeys up to 100 miles will be at 40 pence per mile whilst any distance in excess of this will be at 30 ppm. Hence a journey of 100 miles should cost £40.00 whilst a journey of 200 miles will cost £70.00.
These amendments will require modifications to functions produced for part 1 but the basic structure of the program will remain the same.
Your program should implement the following functions:

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.
a) DispChart: Display the grid in a format similar to fig 2
b) CalcDistance: Allow input of a Journey between two points via up to 3 intermediate 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

Your code should be robust and include appropriate validation and error messages where appropriate.

I have done the coding but cant figurer out why it wont work can any one help plz thank you

Heres the code open attached
• 04-03-2008
JDGATX

Failing that, at least give us some output or some errors.
• 04-03-2008
Salem
You mean such problems as
fp = fopen(Towns.txt, "w");

Not having double quotes around the filename?
• 04-03-2008
Game
Quote:

Originally Posted by Game
You should replace the distances input function with a function to read from a text file containing a list of 10 short place names followed by the requisite number of distances

London Bath Cardiff Carlisle Durham Exeter Leeds Norwich Truro York
London - 23 12 89 456 123 46 732 345 123
Bath 23 - 46 234 123 46 89 234 567 90
Cardiff 12 46 - 767 456 46 234 123 732 35
Carlisle 89 234 767 - 732 32 48 67 98 100
Durham 456 123 456 732 - 234 46 89 89 732
Exeter 123 46 46 32 234 - 123 46 123 234
Leeds 46 89 234 48 46 123 - 46 89 19
Norwich 732 234 123 67 89 46 46 - 123 732
Truro 345 567 732 98 89 123 89 123 - 78
York 123 90 35 100 732 234 19 732 78 -

Fig 2 – Example Data (not accurate)

The details above are for example only and are not accurate distances. You should use different towns/cities and include valid values.

The menu should enable the grid to be displayed and also allow for journeys via intermediate stops to be input thus enabling a route to be calculated, for example, from London to Durham via Norwich and Leeds. In addition it is proposed that two rates of travel be catered for - journeys up to 100 miles will be at 40 pence per mile whilst any distance in excess of this will be at 30 ppm. Hence a journey of 100 miles should cost £40.00 whilst a journey of 200 miles will cost £70.00.
These amendments will require modifications to functions produced for part 1 but the basic structure of the program will remain the same.
Your program should implement the following functions:

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.
a) DispChart: Display the grid in a format similar to fig 2
b) CalcDistance: Allow input of a Journey between two points via up to 3 intermediate 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

Your code should be robust and include appropriate validation and error messages where appropriate.

I have done the coding but cant figurer out why it wont work can any one help plz thank you

Heres the code open attached

Here the error messages attached
• 04-03-2008
vart
Code:

`int ReadChart(const char filename[], Chart* chart)`
Chart should be defined before this line - you do not have this type definition at all

Code:

```[line 15: incompatible types in initializer 'float total = 0'```
your code line 15 does not contains the code above - so show the last code

Code:

```if valid_input = 1;                 else  printf("\tError: Invalid \n");```
should be like
Code:

```if( valid_input != 1)   printf("\tError: Invalid \n");```
or something... I could not get more reasonable code looking on the mess you have here

Code:

`switch (menu != 4)`

count your braces - due to horrible indentation you are missing several, and do not see it
• 04-03-2008
matsp
Can you post the code, as the line numbers don't match with the code posted above [and don't use attachments - it just makes the whole process much more complicated - just paste it in between [ code ] and [ /code ] into your post - minus the spaces.]

--
Mats
• 04-03-2008
Game
Code:

```#include <stdio.h> #include <stdlib.h> #include "string.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 filename[], 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 filename[], 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) {  int  valid_input;  int row,col;  char des1,des2,des3;  int menu;            /* Holds all chices open to the user */   float total = 0;          /* 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 )         {           prinf("\t Please enter first destination point\n");              scanf("%s",des1);           prinf("\t Please enter second destination point\n");              scanf("%s",des2);           prinf("\t Please enter thrid destination point\n");              scanf("%s",des3);                               else                      {                     printf("\tError: Invalid \n");                        }               }   switch (menu != 4)   {     case 1:      FILE *infile;     infile = fopen("Towns.txt", "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");        scanf("%s",des1);     prinf("\t Please enter second destination point\n");        scanf("%s",des2);     prinf("\t Please enter thrid destination point\n");        scanf("%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 /* key > root->data */       return search ( root->right, key );   }   return 0;  }  }```
Made some tweaks hope this is better thank you all for you advise
• 04-04-2008
vart
Code:

```#include <stdio.h> #include <stdlib.h> #include "string.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 filename[], 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 filename[], 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) {         int  valid_input;         int row,col;         char des1,des2,des3;         int menu;            /* Holds all chices open to the user */         float total = 0;          /* 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("&#37;d",&menu);                  /* READ calculation type */         valid_input = 0;         while( valid_input == 0 )         {                 prinf("\t Please enter first destination point\n");                    scanf("%s",des1);                 prinf("\t Please enter second destination point\n");                    scanf("%s",des2);                 prinf("\t Please enter thrid destination point\n");                    scanf("%s",des3);                                 else                                  {                                         printf("\tError: Invalid \n");                                    }         }         switch (menu != 4)         {         case 1:                  FILE *infile;                 infile = fopen("Towns.txt", "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");                    scanf("%s",des1);                 prinf("\t Please enter second destination point\n");                    scanf("%s",des2);                 prinf("\t Please enter thrid destination point\n");                    scanf("%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 /* key > root->data */                                 return search ( root->right, key );                 }                 return 0;         }  }```
So make indentation proper - you will see that the search function is defined inside other function - take it out
• 04-04-2008
matsp
It was pointed out in post #3 that:
Code:

`        fp = fopen(Towns.txt, "r");`
needs " " around the filename. You may want to fix that in both places.

Although I suppose ReadChart and WriteChart are never called, and they write the BINARY form of the data, so the data file should not be called ".txt" - perhaps ".dat" is a better extension - or use the inverse of the code that is in your main(), using fprintf() and loops to output the data.

--
Mats
• 04-09-2008
Game
I have got this prgram to work in miracle C but how do i convert it to get it to work in unix

Code:

```#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("&#37;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.txt", "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 /* key > root->data */       return search ( root->right, key );   }   return 0;  } }```
Node.h file

Code:

```/* utilize an ifndef/define mechanism, so nodes will be define exactly once */ #ifndef _NODE_H #define _NODE_H /* Maximum length of names */ #define strMax 20 /* Define the node structure itself */ struct node { char data [strMax];   struct node * left;   struct node * right; }; #endif```
• 04-09-2008
vart
Quote:

how do i convert it to get it to work in unix
Take unix (linux for example)
Take C compiler (gcc for example)
fix errors and warnings if any
• 04-09-2008
Salem
> I have got this prgram to work in miracle C but how do i convert it to get it to work in unix
Miracle C is a toy, get a real compiler like a GCC port, say code::blocks
• 04-09-2008
BobS0327
• 04-09-2008
Salem
If that college is recommending that compiler, I'm disgusted.
• 04-12-2008
Game
Solved