Thread: Problem in this function:

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    33

    Problem in this function:

    Code:
    void order()
    {
         system("COLOR 0A");
        listc();
    
        struct item food;
        int person_num , amount,i=0 , target[20] ,flag;
    
        FILE *pfile;
        FILE *ofile;
    
        pfile = fopen("food menu.txt","rb");
        ofile = fopen("order.txt","wb");
    
    
    
    
         if(pfile != NULL  &&  ofile != NULL)
                 {
                     printf("Enter number of person-> ");
                     scanf("%d",&person_num);
    
                                     for(;i<person_num;i++)
    
                                      {
                                         printf("\n\nPerson %d enter food serial",i+1);
                                         scanf("%d",&target);
                                         printf("\n\nEnter amount");
                                         scanf("%d",&amount);
                                       printf("Thank You\n\n");
                                       }
    
                 do {
                      fread(&food,sizeof(food),1,pfile);
    
                        if(!feof(pfile))
                          {
                              if ((strcmp) const char*, const char*(target,food.itemnum)==0)
                                    {
                                        for(i<person_num;i++)
    
                                      {
                                         printf("\n\n\n\nPerson %d selected %s",i+1,food.itemname);
    
                                        printf("\nAmount is %d",&amount);
                                       // return SUCCESS;
                                      }
    
           }
       }
       }while(!feof(pfile));
        fclose(pfile);
        fclose(ofile);
                 }
        if(flag==0)
         {
             printf("NO MATCH FOUND");
         }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You could explain what you're trying to do, and what this function does wrongly. We're no psychics...
    How to ask questions the smart way
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    The program is supposed to take input from the file "food menu.txt" and store it in wb mood in"order.txt"

    The problem is it doesn't run. The compiler gives 2 error:
    line 38: expected ')' before const
    line 40: expected ';' before ')' token

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Oh, I see.

    You seem to have two problem in your code. You totally haven't called "strcmp()" correctly on line #38, and on line #40 you forgot to place a semicolor before the comparison.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Code:
    do {                  fread(&food,sizeof(food),1,pfile);
     
                        if(!feof(pfile))
                          {
                              if ((strcmp const char*target, const char*food.itemnum)==0)
    
                                    {
    
                                        for(i<person_num;i++);
    
     
                                      {
    
                                         printf("\n\n\n\nPerson %d selected %s",i+1,food.itemname);
     
                                        printf("\nAmount is %d",&amount);
                                       // return SUCCESS;
                                      }
     
           }
       }
       }while(!feof(pfile));
    still doesn't work
    Last edited by philia; 04-18-2015 at 04:47 AM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yeah, you missed the point entirely. Please have a look at how a function is called and at how a for loop is used.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Code:
    void order()
    {
         system("COLOR 0A");
        listc();
    
        struct item food;
        int person_num , amount,i=0 , target[20] ,flag;
    
        FILE *pfile;
        FILE *ofile;
    
        pfile = fopen("food menu.txt","rb");
        ofile = fopen("order.txt","wb");
    
    
    
    
         if(pfile != NULL  &&  ofile != NULL)
                 {
                     printf("Enter number of person-> ");
                     scanf("%d",&person_num);
    
                                     for( i< person_num; i++)
    
                                      {
                                         printf("\n\nPerson %d enter food serial",i+1);
                                         scanf("%d",&target);
                                         printf("\n\nEnter amount");
                                         scanf("%d",&amount);
                                       printf("Thank You\n\n");
                                       }
    
                 do {
                      fread(&food,sizeof(food),1,pfile);
    
                        if(!feof(pfile))
                          {
                              if ((strcmp const char*target, const char*food.itemnum)==0)
                                    {
                                        for(i<person_num;i++)
    
                                      {
    
    
                                         printf("\n\n\n\nPerson %d selected %s",i+1,food.itemname);
    
                                        printf("\nAmount is %d",&amount);
                                       // return SUCCESS;
                                      }
    
           }
       }
       }while(!feof(pfile));
        fclose(pfile);
        fclose(ofile);
                 }
        if(flag==0)
         {
             printf("NO MATCH FOUND");
         }
    }
    Read them. But I can't get the problem in my code.
    Now line 23 is giving the same error as line 40.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ok, I'm sorry, let me explain it in another way.

    when you call a function, you do it like this:
    Code:
    strcmp(target, food.itemnum)
    when you write a for loop, you must use two semicolors, even if there nothing in between. In other words, the for loop on line #23 was correct, it's line #40 that you needed to change.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Gotcha. Thanks.

    I'm having a bit of problem with another function.
    The function is supposed to edit information in a file. Which it does. But after editing one info, it shows the last item twice.
    Code:
    void edit1()
    {
         int flag =0;
         char target[50];
         struct item food;
        FILE *ptr=fopen("food menu.txt", "rb");
        FILE *ptemp=fopen("temp.txt", "wb");
    
        printf("Enter Item name:\n");
        fflush(stdin);
        gets(target);
    
        if(ptr != NULL && ptemp != NULL)
        {
            do
            {
                fread(&food,sizeof(food),1,ptr);
                if(!feof(ptr))
                {
                    if(strcmp(target,food.itemname)==0)
                    {
    
                            printf("Enter new name:");
                            fflush(stdin);
                            gets(food.itemname);
    
    
                       }
                }fwrite(&food,sizeof(food),1,ptemp);
    
    
    
            }while(!feof(ptr));
            system("pause");
            fclose(ptr);
            fclose(ptemp);
            remove("food menu.txt");
            rename("temp.txt", "food menu.txt");
            system("cls");
    
    }
    else
    {
        system("cls");
    }
    }

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, it does that because you call "fwrite()" outside of the if that checks for EOF. If you move it inside, you probably won't have that problem.

    Also, I just noticed that you use "fflush(stdin);". Don't do that. Its behavior is undefined, meaning it may not do what you think it does.
    Devoted my life to programming...

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  2. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  3. function inside function (most likely) problem
    By vlaskiz in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2011, 05:10 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM