0

Im trying to write code so that it will open a file (worker and benefits) and then display them when asked at the start. I can do this but I would like to use functions. When I run my program it ends as soon as it starts. How do I set up functions so that they will run.

Ive tried renaming the functions to no sucess. Ive also found no help throught Youtube tutorials.

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

int ans;
char Benefits[150];
char Worker[150];

int readfile();
int end();
int welcome();

int main()
{



  int welcome()
  {
    puts("Hi, Welcome to whatever this is!!\n");
  }

  int readfile()
  {



    FILE*fpointer;
    fpointer = fopen("Worker.txt","r");
    char Worker[150];

    while(!feof(fpointer))
    {
      fgets(Worker, 150, fpointer);

    }
    FILE*fpointer1;
    fpointer = fopen("Benefits.txt","r");
    char Benefits[150];

    while(!feof(fpointer))
    {
      fgets(Benefits, 150, fpointer1);

    }
    fclose(fpointer);
  }

  int menu(char Benefits)
  {
    {
      printf("1 - For option 1\n");
      printf("2 - For option 2\n");
      printf("3 - For option 3\n");
      printf("4 - For option 4\n");
      printf("5 - exit\n");

      scanf("%1d", &ans);
    }

    {
      if (ans==1)
        puts(Benefits);

      if (ans==2)
        puts(Worker);

      if (ans==3)
        puts("This is option3");

      if (ans==4)
        puts("This is option4");
    }
  }

  return 0;
}

I expect the output to print either of the files or exit. As of now It skips functions and ends the program.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 10
    You have a bunch of *nested* function *definitions* without a single call. Let's start from the fact that nesting functions is not allowed in standard C. – Eugene Sh. May 08 '19 at 15:01
  • @EugeneSh. and, maybe that point to the need for OP to go back to a good book. – Sourav Ghosh May 08 '19 at 15:09
  • @EugeneSh. How does this even [compile](https://gcc.godbolt.org/z/eDv0vB)? – Aykhan Hagverdili May 08 '19 at 15:09
  • Hi Patrick. Looks like you have the syntax down and can write functions, but you are struggling with some fairly fundamental concepts in calling functions. I would recommend a [good book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – MFisherKDX May 08 '19 at 15:21
  • 1
    Also, can I ask what book or tutorial told you to write this: `while(!feof(fpointer)) { fgets(Worker, 150, fpointer); }`? If you got this from a book, please do yourself a favor, throw the book away, read this: https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong – MFisherKDX May 08 '19 at 15:24
  • @MFisherKDX, thanks for the comments. It was just a self test too see if I could do a few random tasks. I took elements from my universities past exam papers. It seems to be a deeply rooted problem. Ill have a look at the book and start from basics again and build it up again. Its only a small part of my engineering degree so ill revisit this problem after my exams. – Patrick Kelleher May 08 '19 at 16:14
  • You also shadow `Worker` and `Benefits` in a way that's certainly not what you want. – Neil May 08 '19 at 21:10

1 Answers1

1

The functions should be outside the main function. In your main function you call the functions as required.

int main()
{
   welcome();
   readfile();

etc.

   return 0;
}

To be really useful functions can take parameters. If you define readfile like this you declare a parameter called filename that contains the name of the file. You can use this inside your function instead of writing the exact name of the file.

int readfile (char *filename)
{
  ...
  fpointer = fopen(filename,"r");
  ...
}

Now in main you can use

int main()
{
   welcome();
   readfile("Workers.txt");
   ...
}

This is what makes functions useful. You can now reuse the function for another file with a different name. This isn't a solution for you but I hope it helps, even if just with your understanding.

Jon Guiton
  • 1,360
  • 1
  • 9
  • 11