Having problem with C program plz help

This is a discussion on Having problem with C program plz help within the C Programming forums, part of the General Programming Boards category; You should replace the distances input function with a function to read from a text file containing a list of ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    7

    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.
    2) Menu: A menu to support the following:
    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
    Attached Files Attached Files

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I'm pretty sure someone already posted about this exact assignment. Go run a search.

    Failing that, at least give us some output or some errors.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,446
    You mean such problems as
    fp = fopen(Towns.txt, "w");

    Not having double quotes around the filename?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    Quote Originally Posted by Game View Post
    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.
    2) Menu: A menu to support the following:
    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
    Attached Files Attached Files

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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)
    better reread the switch description


    count your braces - due to horrible indentation you are missing several, and do not see it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    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

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    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

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    how do i convert it to get it to work in unix
    Take unix (linux for example)
    Take C compiler (gcc for example)
    ompile your code
    fix errors and warnings if any
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,446
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,446
    Yes, same IP address too.
    If that college is recommending that compiler, I'm disgusted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    Solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 09:45 AM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21