[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Compiler errors

Dear experts I have written the following code:

#include <stdio.h>

main()
{
     FILE *fp;/*creates a file pointed named fp*/
     char places[100];
     int distances[10][10];
     struct places
    {
       char places[10];
     }
     int menu;
     char userinput1[10];
     char userinput2[10];
     int cost;
     int other;
     int a;
     int b;
     struct places names[10];

/* reading in data from file*\
  do
  {
     printf("Enter file name");
     scanf("%s", places);
     fp = fopen(places,"r");
     if (fp == NULL)
     printf("File %s does not exist\n",place);
  }
  while(fp ==NULL);

  while(feof(fp) ==0)
{
   for(int i = 0; i < 10; i++)
   {
     fscanf(fp, "%s", &names[i].places);
   }
     for(int i = 0; i < 10; i ++)
     {
       for(int j = 0; j < 10; j++)
       {
         fscanf(fp, "%d", &distances[i][j]);
       }
     }

}


/* menu */

do
{
   printf("what would you like to do\n");
   printf("option 1:display mileage table");
   printf("option 2: calulate distances between points");
   printf("option 3: calulate cost of journy");
   printf("option 4: exit");
   scanf("%d", &menu);

 switch(menu)
 {
  case 1:
         display(distances, names);
         break;
  case 2:
        distance(distances, names);
        break;
  case 3:
        calc(distances,names);
        break;
  case 4:
        exit(0);
        break;
 }

}
while(menu<5 && menu > 0);

}

/* display mileage chart*/

display( distances[10][10], struct places names[10])
{
printf(" mileage chart");
printf("Burton Derby Swad Stoke Staff Lincoln Walsall Telford Cannock Warwick”)

for(int i = 0; i<10; i++)
{
  for(int j = 0; j < 10; j++)
  {
   printf("%7s",names[i].places);
   printf("%7d", distances[i][j]);

   }
}
}

/*distance between points*/
distance( distances[10][10], struct places names[10])
{

printf("Burton");
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf("Warwick");

printf("Enter the first point from the above list");
scanf("%s", &userinput1);

for (int i = 0; i<10; i++)
{
  if(strcmp(userinput1, names[i].places) ==0)
  a = i;
 
   else
   {
        printf("Please enter a place from the list");
   }

printf("Enter the second point from the above list");
scanf("%s", &userinput2);

for(int j = 0; j <10;j++)
{
 if(strcmp(userinput2, names[j].places) == 0)
b = j;

else
{
     printf("Please enter a place from the list");
}

printf("The distance is %d", distances[a][b]);

}

/*calculations between points*/
calc( distances[10][10], struct places names[10])
{
printf("Burton");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf(“Warwick”);
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");

printf("Enter the first point from the list");
scanf("%s", userinput1);

for (int i = 0; i <10; i++)
{
  if(strcmp(userinput1, names[i].places) == 0)
  a = i;

  else
  {
    Printf(“Please enter a point from the list”);
  }
}


printf("Enter the second point from the list");
scanf("%s", userinput2);

for(int j = 0; j < 10; j++)
{
   if(strcmp(userinput2, names[j].places) ==0)
   b = j;

   else
   {
      Printf(“Enter a point from the list”);
   }
}

if(distances[a][b] <= 100)
{
    cost = (distances[a][b] * 40)/100;
     printf("The cost is %d", cost);
}
else
{
   other = distances[a][b] - 100;
   other = other * 30;
    cost = (100 * 40) + other;
     cost = cost/100;
      printf("The cost of the journy is %d", cost);
  }
}

The following errors occur:
cc: Error: mile.c, line 87: Error parsing parameter list. Found "[" when expecting one of: ",
", ")". (notexpecting)
display( distances[10][10], struct places names[10])
------------------^
cc: Error: mile.c, line 108: Error parsing parameter list. Found "[" when expecting one of: "
,", ")". (notexpecting)
distance( distances[10][10], struct places names[10])
-------------------^
cc: Error: mile.c, line 147: Error parsing parameter list. Found "[" when expecting one of: "
,", ")". (notexpecting)
calc( distances[10][10], struct places names[10])
---------------^
cc: Error: mile.c, line 12: In this declaration, "struct" cannot be combined with "int". (typ
econflict)
  int menu;
--^
cc: Error: mile.c, line 68: In this statement, "names" is not declared. (undeclared)
       display(distances, names);

Thank you in advance for your help
0
Halt123
Asked:
Halt123
  • 4
  • 2
3 Solutions
 
jkrCommented:
You are missing the argument type and the return type, ie.g.

/* display mileage chart*/
void display( int distances[10][10], struct places names[10])
{
/* ...*/

/*distance between points*/
void distance( int distances[10][10], struct places names[10])
{
/* ...*/

/*calculations between points*/
void calc( int distances[10][10], struct places names[10])
{
/* ...*/
0
 
Halt123Author Commented:
I've changed that which has got rid of those errors, howerve its still bringing up lots of errors to do with the names varaible
cc: Error: mile.c, line 8: In this declaration, "struct" cannot be combined with "int". (type
conflict)
  int menu;
--^
cc: Error: mile.c, line 69: In this statement, "names" is not declared. (undeclared)
       display(distances, names);
--------------------------^
cc: Info: mile.c, line 88: The type "struct places" has been declared within and is limited t
o a function prototype scope.  It will not be compatible with an identical type declared in a
nother scope.  This might not be what you intended. (protoscope)
 display(int distances[10][10], struct places names[10])
--------------------------------^
cc: Error: mile.c, line 98: In this statement, "names" does not point to an object type. (nee
dptrobj)
   printf("%7s",names[i].places);
----------------^
cc: Info: mile.c, line 110: The type "struct places" has been declared within and is limited
to a function prototype scope.  It will not be compatible with an identical type declared in
another scope.  This might not be what you intended. (protoscope)
 distance(int distances[10][10], struct places names[10])
---------------------------------^
cc: Error: mile.c, line 129: In this statement, "names" does not point to an object type. (ne
edptrobj)
  if(strcmp(userinput1, names[i].places) ==0) /*userinput1 is equal to names[i].places*/
------------------------^
cc: Error: mile.c, line 143: In this statement, "names" does not point to an object type. (ne
edptrobj)
 if(strcmp(userinput2, names[j].places) == 0)
-----------------------^
cc: Info: mile.c, line 158: The type "struct places" has been declared within and is limited
to a function prototype scope.  It will not be compatible with an identical type declared in
another scope.  This might not be what you intended. (protoscope)
 calc(int distances[10][10], struct places names[10])
-----------------------------^
cc: Error: mile.c, line 176: In this statement, "names" does not point to an object type. (ne
edptrobj)
if(strcmp(userinput1, names[i].places) == 0)
----------------------^
cc: Error: mile.c, line 186: In this statement, "names" does not point to an object type. (ne
edptrobj)
if(strcmp(userinput2, names[j].places) ==0)

thank you
0
 
imladrisCommented:
A lot of those messages are caused by the fact that "struct places" is declare *inside* main. When other functions are declared, they cannot "see" stuff that has been done inside another function. To rectify that, lift the declaration of struct places outside/above main.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Halt123Author Commented:
the errors I get now are:

cc: Error: mile.c, line 8: In this declaration, "struct" cannot be combined with "int". (type
conflict)
  int menu;
--^
cc: Error: mile.c, line 98: In this statement, "names" does not point to an object type. (nee
dptrobj)
   printf("%7s",names[i].places);
----------------^
cc: Error: mile.c, line 129: In this statement, "names" does not point to an object type. (ne
edptrobj)
  if(strcmp(userinput1, names[i].places) ==0) /*userinput1 is equal to names[i].places*/
------------------------^
cc: Error: mile.c, line 143: In this statement, "names" does not point to an object type. (ne
edptrobj)
 if(strcmp(userinput2, names[j].places) == 0)
-----------------------^
cc: Error: mile.c, line 176: In this statement, "names" does not point to an object type. (ne
edptrobj)
if(strcmp(userinput1, names[i].places) == 0)
----------------------^
cc: Error: mile.c, line 186: In this statement, "names" does not point to an object type. (ne
edptrobj)
if(strcmp(userinput2, names[j].places) ==0)
----------------------^
cc: Error: mile.c, line 16: The type of the tentatively-defined variable "names" is incomplet
e at the end of the compilation unit. (incomptent)
struct places names[10];

thank you for ur help
0
 
imladrisCommented:
The first error was originally cause by the fact that the definition for "struct places" was not ended with a semi-colon. However, I'm puzzled as to why that would still show up as tripping on "int menu". Did you not lift struct places out of main? Did you take int menu with it?

I don't have an explanation for the names errors, off hand. The error hing (needptrobj) appears to mean "need pointer object". But names seems to be one. The problem may be connected to the places declaration/definition missing the semi-colon.

Give it another whirl; think about the errors you're getting. If you have more questions, please post the code you currently have.
0
 
Halt123Author Commented:
Its still bringing up the errors

the code is:
#include <stdio.h>
char places[100];
     int distances[10][10];
     struct places;
    {
       char places[10];
     }
     int menu;
     char userinput1[10];
     char userinput2[10];
     int cost;
     int other;
     int a;
     int b;
     struct places names[10];

main()
{
     FILE *fp;/*creates a file pointed named fp*/
     
/* reading in data from file*\
  do
  {
     printf("Enter file name");
     scanf("%s", places);
     fp = fopen(places,"r");
     if (fp == NULL)
     printf("File %s does not exist\n",place);
  }
  while(fp ==NULL);

  while(feof(fp) ==0)
{
   for(int i = 0; i < 10; i++)
   {
     fscanf(fp, "%s", &names[i].places);
   }
     for(int i = 0; i < 10; i ++)
     {
       for(int j = 0; j < 10; j++)
       {
         fscanf(fp, "%d", &distances[i][j]);
       }
     }

}


/* menu */

do
{
   printf("what would you like to do\n");
   printf("option 1:display mileage table");
   printf("option 2: calulate distances between points");
   printf("option 3: calulate cost of journy");
   printf("option 4: exit");
   scanf("%d", &menu);

 switch(menu)
 {
  case 1:
         display(distances, names);
         break;
  case 2:
        distance(distances, names);
        break;
  case 3:
        calc(distances,names);
        break;
  case 4:
        exit(0);
        break;
 }

}
while(menu<5 && menu > 0);

}

/* display mileage chart*/

display( distances[10][10], struct places names[10])
{
printf(" mileage chart");
printf("Burton Derby Swad Stoke Staff Lincoln Walsall Telford Cannock Warwick”)

for(int i = 0; i<10; i++)
{
  for(int j = 0; j < 10; j++)
  {
   printf("%7s",names[i].places);
   printf("%7d", distances[i][j]);

   }
}
}

/*distance between points*/
distance( distances[10][10], struct places names[10])
{

printf("Burton");
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf("Warwick");

printf("Enter the first point from the above list");
scanf("%s", &userinput1);

for (int i = 0; i<10; i++)
{
  if(strcmp(userinput1, names[i].places) ==0)
  a = i;
 
   else
   {
        printf("Please enter a place from the list");
   }

printf("Enter the second point from the above list");
scanf("%s", &userinput2);

for(int j = 0; j <10;j++)
{
 if(strcmp(userinput2, names[j].places) == 0)
b = j;

else
{
     printf("Please enter a place from the list");
}

printf("The distance is %d", distances[a][b]);

}

/*calculations between points*/
calc( distances[10][10], struct places names[10])
{
printf("Burton");
printf("Lincoln");
printf("Walsall");
printf("Telford");
printf("Cannock");
printf(“Warwick”);
printf("Derby");
printf("Swad");
printf("Stoke");
printf("Stafford");

printf("Enter the first point from the list");
scanf("%s", userinput1);

for (int i = 0; i <10; i++)
{
  if(strcmp(userinput1, names[i].places) == 0)
  a = i;

  else
  {
    Printf(“Please enter a point from the list”);
  }
}


printf("Enter the second point from the list");
scanf("%s", userinput2);

for(int j = 0; j < 10; j++)
{
   if(strcmp(userinput2, names[j].places) ==0)
   b = j;

   else
   {
      Printf(“Enter a point from the list”);
   }
}

if(distances[a][b] <= 100)
{
    cost = (distances[a][b] * 40)/100;
     printf("The cost is %d", cost);
}
else
{
   other = distances[a][b] - 100;
   other = other * 30;
    cost = (100 * 40) + other;
     cost = cost/100;
      printf("The cost of the journy is %d", cost);
  }
}




0
 
Halt123Author Commented:
I've just found where the error were

thank you for your help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now