Thread: some help maybe...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    some help maybe...

    Code:
    #include <stdio.h>
    #include <string.h>
    #define ROWS 10
    #define COLS 10
    #define PLACE 15
    
    typedef struct
    {
       char placeName[PLACE];
       int miles[10];
    }distances;
    
    int menu(void);
    void calcDistance(distances []);
    void totalCost(distances []);
    void dispChart(distances []);
    void readFile(distances []);
    
    int main()
    {
       int choice;
       distances places[10];
       readFile(places);
    
       choice=menu();
       while(choice !=4)
       {
         switch(choice)
         {
          case 1:/*display distance*/
                dispDistance(places);
          break;
          case 2:/*calculate cost of journey*/
                journeyCost(places);
          break;
          case 3:/*display the chart*/
                dispChart(places);
          break;
          default:
              puts("Please enter a valid number");
         }/*end switch*/
        choice=menu();
        }/*end while*/
    
          return 0;
    }
    
    int menu(void)
    {
          int menuChoice;
          puts("\n");
          puts("Menu");
          puts("1) Display distance between two places");
          puts("2) Calculate the cost of a journey");
          puts("3) Display the mileage chart");
          puts("Please select your choice or enter 0 to quit:");
          scanf("%d",&menuChoice);
          fflush(stdin);
    
          return menuChoice;
    }/*end menu*/
    
    void calcDistance(distances places[])
    {
          int loop;
          char start,end;
          printf("Enter the name of the starting location: ");
    	  scanf("%d",&start);
    
          printf("Enter the next location or exit to return to the main menu: ");
          scanf("%d",&end);
    
          do
            {
              if x[loop+1] !=-1
              distances[row].miles[col]
              loop++
             }/*end do*/
           while loop<5;
          return;
    }/*end calcDistance*/
    
    void totalCost(distances places[])
    {
        /*not sure how to call dispDistance*/
        int dist;
        float cost;
    
        if(dist <=100)
        {
           cost = dist * 0.4;
        }
        else if(dist >100)
        {
           cost =(100 * 0.4)+((dist-100)*0.3);
        }
    	printf("Cost of the journey is: %f\n", cost);
    
    }/*end totalCost*/
    
    void dispChart(distances places[])
    {
        int row,col;
        puts("Mileage chart");
          for(row=0;row<ROWS;row++)
          {
             puts("");/*New line before each row*/
             printf("%s\t",places[row].placeName);/*offset by 1(so 1,2,3,4,5 instead of 0,1,2,3,4)*/
    
             for(col=0;col<COLS;col++)
             {
                  printf("%d\t",places[row].miles[col]);
             }/*end for*/
          }/*end for*/
    
    }/*end dispChart*/
    
    void readFile(distances places[])
    {
     int loop;
     int innLoop;
     FILE *fp;
     fp=fopen("distance_chart.txt","r");
     if(fp==NULL)
     {
       puts("\nFile not opened");
     }/*end if*/
     else
     {
       puts("\nOutput.....");
       for(loop=0;loop<10;loop++)
       {
         fscanf(fp,"%s",places[loop].placeName);
         for(innLoop=0;innLoop<10;innLoop++)
         {
         fscanf(fp,"%d",&places[loop].miles[innLoop]);
         }/*end for*/
    
    
       }/*end for*/
    
       if(fclose(fp)!=0)
       {
          puts("\nError closing file");
       }/*end if*/
      }/*end else*/
    }/*end readFromFile*/
    
    int placeSearch (distances places[])
    {
       char place[PLACE];
       int loop;
    
       scanf("%s",place);
    
       for(loop=0;loop<10;loop++)
       {
          if(strcmp(place,places[loop].placeName)==0)
          {
          return loop;
          }
       }
       return -1;
    
    }/*end placeSearch*/

    In calcDistance I can't get the program to display the distance between two places?? Any suggestions??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you can post code that actually compiles, as (at least) the calcDistance function has syntactical errors that do not allow it to compile.

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    For starters, loop is not initialized, and row and col are not defined.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Calculating distance requires subtracting things. How were you planning on calculating distance?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    5
    Code:
    int calcDistance(distances places[])
    {
          int loop;
          int x[10];
          int start,end;
          printf("Enter the name of the starting location: ");
    	  scanf("%d",&start);
    
          printf("Enter the next location or exit to return to the main menu: ");
          scanf("%d",&end);
    
    
          return distances[start].miles[end];
    }/*end calcDistance*/
    Code:
    int placeSearch (distances places[])
    {
       char place[PLACE];
       int loop;
    
       scanf("%s",place);
    
       for(loop=0;loop<10;loop++)
       {
          if(strcmp(place,places[loop].placeName)==0)
          {
          return loop;
          }
       }
       return -1;
    
    }/*end placeSearch*/
    The issue I have here is calling the placeSearch function from calcDistance and assigning the index retured to start/end respectively?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what do you think you need to change in your calcDistance function to make it call the placeSearch function?

    --
    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.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    5
    Code:
    scanf("%d",&start);
    
               scanf("%d",&end);
    I think it should be something on these lines?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by uncle_al View Post
    Code:
    scanf("%d",&start);
    
               scanf("%d",&end);
    I think it should be something on these lines?
    Yes, that would probably help. And what do you think will do the right thing here?

    --
    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
    May 2008
    Posts
    5
    Code:
    int calcDistance(distances places[])
    {
          int loop;
          int x[10];
          int start,end;
          printf("Enter the name of the starting location: ");
    	  scanf("%d",&start[loop]);
    
          printf("Enter the next location or exit to return to the main menu: ");
          scanf("%d",&end[loop]);
    
    
          return distances[start].miles[end];
    }/*end calcDistance*/
    ?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by uncle_al View Post
    Code:
    int calcDistance(distances places[])
    {
          int loop;
          int x[10];
          int start,end;
          printf("Enter the name of the starting location: ");
    	  scanf("&#37;d",&start[loop]);
    
          printf("Enter the next location or exit to return to the main menu: ");
          scanf("%d",&end[loop]);
    
    
          return distances[start].miles[end];
    }/*end calcDistance*/
    ?
    And how is that making use of the searchPlace function?

    Not to mention that it won't compile and loop is uninitialized.

    --
    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.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    5
    Code:
    int calcDistance(distances places[])
    {
          int loop;
          int x[10];
          int start,end;
          printf("Enter the name of the starting location: ");
    	  scanf("%d",&start[places]);
    
          printf("Enter the next location or exit to return to the main menu: ");
          scanf("%d",&end[places]);
    
          return distances[start].miles[end];
    }/*end calcDistance*/

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by uncle_al View Post
    Code:
    int calcDistance(distances places[])
    {
          int loop;
          int x[10];
          int start,end;
          printf("Enter the name of the starting location: ");
    	  scanf("%d",&start[places]);
    
          printf("Enter the next location or exit to return to the main menu: ");
          scanf("%d",&end[places]);
    
          return distances[start].miles[end];
    }/*end calcDistance*/
    It seems like you randomly change your code without actually trying to compile it. It would help if the code you post is at least compilable and doesn't contain syntactically incorrect C code.

    --
    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.

Popular pages Recent additions subscribe to a feed