Solved

Compiling C With The ANSI Standard - Unresolved Errors

Posted on 1999-01-26
19
287 Views
Last Modified: 2012-05-04
I have a short program (184 lines) that will compile in Borland's Turbo C++ vs 4.5.  When I changed the language compliance option from Borland extensions to ANSI, I ended up with errors I can't resolve.

Can you resolve the errors (5) and is there any reference online or anywhere that will show these differences?

I can send the code via e-mail.  Thanks
0
Comment
Question by:John500
  • 11
  • 3
  • 3
  • +2
19 Comments
 
LVL 1

Expert Comment

by:FuzzyLogic
Comment Utility
What are the error massages you are getting?
0
 

Author Comment

by:John500
Comment Utility
I had one unresolved error compiling without ANSI
Error PARTLIST.C 84: If statement missing ) in function searchPart

Here they are the ANSI code errors. The actual code was e-mailed:

Compiling PARTLIST.C:
Error PARTLIST.C 18: Type name expected
Error PARTLIST.C 18: Declaration terminated incorrectly
Error PARTLIST.C 30: Declaration terminated incorrectly
Error PARTLIST.C 53: Unterminated string or character constant
Error PARTLIST.C 53: Character constant must be one or two characters long
Error PARTLIST.C 83: Unterminated string or character constant
Error PARTLIST.C 83: Character constant must be one or two characters long

0
 

Author Comment

by:John500
Comment Utility
FuzzyLogic,

Let me know if you get the code I e-mailed.  Thanks
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
Can you pls post your code.
0
 

Author Comment

by:John500
Comment Utility
Sure, here it is:

/* John Barbacci
   1/28/99
   This program demonstrates linked-list processes.  The list is
   a standard singly-linked list using dynamically allocated
   nodes.

*/

/*------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*------------------------------------------------*/



      struct part{              // node structure definition
      char partNo[6];
      int count;
      struct part *next;
      };

      struct list{
            struct part *head;
      };



struct part * getPart(struct list *partlist, char *pNew, int cnt)
      {
            struct part *p;
            p = (struct part*) malloc(sizeof(struct part));

            if(partlist == NULL){
                  printf("Memory failure in getPart");
            } else {

                  strcpy(p->partNo, pNew);
                  p->next = NULL;
                  p->count = cnt;
              }
                  return p;
      

int searchPart(struct list *partlist, char *pValue, int cnt)
 {
    int insertPart(struct list *partlist, char *pValue, int cnt);

      struct part *q;
      q = partlist;

// if the list is empty, or the head of list is equal to part
      if      ((q == NULL) ||(strcmp(q->partNo, pValue)== 0)){

            if(q == NULL){
            insertPart(partlist, pValue, cnt);
            return;
            }
      else{ // head of list is equal to pValue - increase cnt
            q->count = q->count + cnt;
            return;
            }
      }
      else{   // search the list for the part number

      while       // while parts are not equal to pValue
      (q->next != NULL && strcmp(q->next->partNo, pValue)!= 0)
            q = q->next;

            // if the list doesn't contain the part
if((q->next == NULL) && (strcmp(q->next->partNo, pValue)!= 0){
                  insertPart(partlist, pValue, cnt);
                  return;
            }

            else    //  a part is equal to pValue
                  q->next->count = q->next->count + cnt;
            } return; // end of if/else
      }  //  end searchPart

        /*****  Insert Function   *****/
int insertPart(struct list *partlist, char *pValue, int cnt)
      {
      struct part *q, *pNew;

      pNew = getPart(partlist, pValue, cnt);

        // if no memory, error printed in getPart()
      if(pNew == NULL)    
            return;
      else{
            // while the part is not equal to pValue
            while(strcmp(q->next->partNo, pValue) != 0)
                  q = q->next;

                // insert part at the end of list
            if(q->next == NULL){  
                  pNew->next = NULL;
                  q->next = pNew;
            }
            else{    // insert part in proper order
                  pNew->next = q->next;  
                  q->next = pNew;
            }

      } return;      // end if
}  //  end insertPart




      void readFile(void)
      {

      FILE *fopen(), *inputFile;        / input file pointer

      char fname[40];
      char buff[20];
      char type[3];
      char part[14];
      int count;
      int line = 0;


      printf("Enter a file to read:\n\n");

      scanf(" %s", fname);
      inputFile = fopen( fname, "r" );
            if( inputFile == NULL ) {
              printf("Unable to open datafile %s\n", fname );
              return;
            }

            while(!feof(inputFile)){

                      fgets(buff, 20, inputFile);
                line++;
                sscanf(buff, "%s %s %i", type, part, &count);

            if (strcmp(type, "D")== 0){ //if delivery
                 searchPart(part, count, count);    
                 printf("Inserting:  %s  %i\n", part, count);

            }
            else if(strcmp(type, "O")== 0){  // if order
            //insertPart(part, quantity);    
            printf("Removing:  %s  %i\n", part, count);
            }
            else{
               printf("Invalid Operation Code: %s\n", type);
             printf("%s - invalid part number!\n", type);
                 break;
            }
      } // end of while
}  //end readFile

      void main (void)
      { // Display the menu and get the entry: r, d, p, s, or q
            void readFile(void);
            //struct list  PartList = {NULL};

        char response='b';   // initialize to start while loop
        while ((response != 'q') && (response != 'Q'))
        { flushall();
            printf("\n\n");
            printf("r)ead,        p)print list      q)uit,\n");
            printf("d)elete,  s)earch part\n");
            printf("Selection:\n\n");
             response = getchar();
            flushall(); // flush I/O buffers - in case more than one char was entered
            printf("\n");

      if ((response != 'q') && (response != 'Q'))
      {
            switch (response)
            {
                  case 'r':
                  case 'R': // Access Input File
                                    readFile();
                                    break;
                  case 's':
                  case 'S': // Search List
                  {
                        
                  //searchPart();
                  }break;
              case 'd':
              case 'D': // Delete Part
                  {
                        break;
                                    }
                        //deletePart();
                  } break;

                    case 'n':
                    case 'N': // Print List
                        printList();
                                    break;   */
                    default:  break;
                  }   // end switch
            }  // end if
       }    // end while
      }   // end main






0
 
LVL 11

Expert Comment

by:mouatts
Comment Utility
// is not ANSI C actually its not C its C++ therefore is not being interpreted as a comment thus the bit that says
//if delivery
will be interpreted as an if statement.
also you have a comment
with
/ input file pointer
again this should be a /**/ pair and certainly not a single /

Its differcult to tell is this accounts for all the errors but it seems likely that it does.

HTH
Steve
0
 

Author Comment

by:John500
Comment Utility
I went through and replaced all the // marks with /* */.  However, I ended up with quite a few errors.  Can you give me the option to edit the question so that I can replace the code and show some errors?  Thanks.

Before you answered this question I tried to edit it but kept getting an error.  Hopefully that won't happen again.
0
 
LVL 11

Expert Comment

by:mouatts
Comment Utility
Sorry I'm not sure what you are asking? Can't you just post your code (and errors) as another comment?

Steve
0
 

Author Comment

by:John500
Comment Utility
Steve,

The code below compiles with the changes in // syntax but the logic is what is messing up at this point of the readFile func:

if (inventory)
   ptr = searchList(inventory, part);

   if (ptr = 0){                                 
      if (strcmp(type, "D")== 0)       /* if delivery  */
      insertPart(inventory ,part, count);  /* place in list */

.

Can you answer this question as part of the previous? Here is the whole shi-bang:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*------------------------------------------------*/



      struct part{              /* node structure definition */
            char partNo[6];
            int count;
            struct part *next;
      };

      struct list{
            struct part *head;
      };

      void printParts(struct list *inventory)
      {
      struct part *Cur = (struct part*)  inventory->head;

      while(Cur){
      printf("In stock %i of part %s\n", Cur->count, Cur->partNo);
            Cur = Cur->next;
            } return;
      }


      struct part * getPart(char *pNew, int cnt)
      {
            struct part *p;
            p = (struct part*) malloc(sizeof(struct part));

            if(!p){
                  printf("Memory failure in getPart");
            } else {

                  strcpy(p->partNo, pNew);
                  p->next = NULL;
                  p->count = cnt;
              }
                  return p;
      }

      struct part *searchList(struct list *inventory, char *pValue)
      {
            struct part *Cur = (struct part *)inventory->head;

            while(Cur){
                  if ((strcmp(Cur->partNo, pValue)== 0))
                         return Cur;
                         Cur = Cur->next;
            } return Cur;
      }


      int insertPart(struct list *inventory, char *pValue, int cnt)
      {
            struct part *pNew;
            struct part *NxtPtr = inventory->head;
            struct part *PrvPtr = NULL;

            pNew = getPart(pValue, cnt);

            if(pNew == NULL)     /* if no memory, error printed in getPart() */
                  return 0;

            else{

                    while (NxtPtr){  /* While there are parts to test. */

                              if (strcmp(NxtPtr->partNo, pValue) > 0)
                                          break;
                                    PrvPtr = NxtPtr;       /*  Save -> current part */
                                    NxtPtr = NxtPtr->next;
                        }


                              if (PrvPtr != NULL){        /* adding to the middle &  tail */
                                    pNew->next = NxtPtr;
                                    PrvPtr->next = pNew;
                              }


                              else{
                                    inventory->head = pNew;    /* adding to the head  */

                                    printf("%i of part %s added\n", cnt, pValue);

                              }

            }   return 1;
      }    /* end of insertPart()  */



      void readFile(void)
      {

            /*struct part *searchList(struct list *prtlist, char *pValue);*/
            FILE *fopen(), *inputFile;              /* input file pointer  */
            struct list *inventory = NULL;

            char fname[40];
            char buff[20];
            char type[3];
            char part[14];
            int count;
            int line = 0;
            struct part *ptr;

            printf("Enter a file to read:\n\n");
            scanf(" %s", fname);
            inputFile = fopen(fname, "r");

                        if( inputFile == NULL ) {
                        printf("Unable to open datafile %s\n", fname );
                        return;
                  }

            while(!feof(inputFile)){

                        fgets(buff, 20, inputFile);
                        line++;
                        sscanf(buff, "%s %s %i", type, part, &count);

                        if((strcmp(type, "D")!= 0) && ((strcmp(type, "O")!= 0)))
                              printf("%s - invalid code!\n", type);

                        else{

                              if (inventory)
                                    ptr = searchList(inventory, part);

                              if (ptr = 0){

                                    printf("Look at part %s %i\n", part, count);
                                    if (strcmp(type, "D")== 0)       /* if delivery  */
                                          /*printf("Inserting part %s %i\n", part, count); */
                                          insertPart(inventory ,part, count);  /* place part in list */
                                    else
                                          printf("An order for %i units of part %s\n", count, part);
                                          printf("cannot be processed - this part is not in stock\n");
                              }
                              else{
                                    if(strcmp(type, "D")== 0)        /* if delivery */
                                          printf("Adding part %s %i\n", part, count);
                                          /*addCount(inventory, part, count)   increase part count */


                                    else                             /* if order */
                                          printf("Subtracting part %s %i\n", part, count);
                                          /*subtCount(inventory, part, count) /* reduce part count */

                              }       printParts(inventory);
                        }  /* end if/else type comparison  */
            } /* end of while  */
      }  /*end readFile  */

      main()
      {
        /* Display the menu and get the entry: r, d, p, s, or q   */

        void readFile(void);
        struct list inventory = {NULL};

        char response='b';   /* initialize to start while loop */
        while ((response != 'q') && (response != 'Q'))
        { fflush(stdin);
            printf("\n\n");
            printf("r)ead,            p)print list      q)uit,\n");
            printf("d)elete,      s)earch part\n\n");
            printf("Selection:\n\n");
             response = getchar();
            fflush(stdin); /* flush I/O buffers - in case more than one char was entered */
            printf("\n");

      if ((response != 'q') && (response != 'Q'))
      {
            switch (response)
            {
                  case 'r':
                  case 'R': /* Access Input File */
                                    readFile();
                                    break;
                  case 's':
                  case 'S': /* Search List */

                                    break;
              case 'd':
              case 'D': /* Delete Part  */

                                    break;

                    case 'n':
                    case 'N': /* Print List */

                                    break;
                    default:  break;
                  }   /* end switch  */
            }        /* end if   */
       }    /* end while  */
      }   /* end main  */
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:John500
Comment Utility
In addition,

I have declared the singly linked list in the main as:
struct list Inventory = {NULL};

This creates a variable of type list and sets its head pointer to NULL.

Within the readFile function I declare:
struct list *Inventory = NULL;

this creates a pointer that can point to a variable of type list.  Currently it points to NULL.

If this is right, how come the readFile code which I mentioned above, doesn't do the right job by allowing the empty list to begin the insertion code.  Can you make the change?

0
 

Author Comment

by:John500
Comment Utility
Lost participation from expert
0
 

Author Comment

by:John500
Comment Utility
Steve/anyone,

I didn't want to post all the code again but I did that in part to respond to rbr.  I would rather e-mail code if it's that long.  However, since I was having trouble compiling in ANSI C I thought you guys wanted the whole thing.  Anyway, I've over come that problem.

How about the 60 points for the following:  I'm trying to parse an incoming file of words.  I'm using strtok.  When I've used strtok in the past, I've always known how many items would be coming in from the file per line and could therefore set the succeeding calls to strtok accordingly.

Otherwise how do you set a loop to take care of the calls to strtok after the initial call.  Here is what I have, but it's not working:

while(!feof(inputFile)){
      fgets(buff, 40, inputFile);

      token = strtok(buff, " \n");
      insertNode(List, token);

      while(strtok(buff, " \n") != NULL){
         token = strtok(0, " \n");
         insertNode(List, token);
      }
}


0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
while( token = strtok(0, " \n") ){
    insertNode(List, token);
}
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
> if (ptr = 0){
Did you mean == ?
0
 

Author Comment

by:John500
Comment Utility
ozo,

Give me the option to grade, I'll give you the points.  That was a good idea to say:

while( token = strtok(0, " \n") ){

For as long as I looked at this routine, you would think I could come up with that.  I finally got the following to work but yours would be better:

while(!feof(inputFile)){
      fgets(buff, 40, inputFile);

      for (s = buff, space_count = 0; *s; s++){
          if (*s == ' ')
          space_count++;
      }
      token = strtok(buff, " ");
      insertNode(List, token);

      while(space_count != 0){
          token = strtok(0, " ");
          insertNode(List, token);
          space_count--;
      }
}
0
 
LVL 84

Accepted Solution

by:
ozo earned 60 total points
Comment Utility
ok.
0
 

Author Comment

by:John500
Comment Utility
Thanks
0
 
LVL 11

Expert Comment

by:mouatts
Comment Utility
sorry that I didn't answer but I have been away.

Steve
0
 

Author Comment

by:John500
Comment Utility
No problem...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now