0

I've read other questions regarding this problem but I still don't understand how I would create a prototype for both of these functions. I am currently using Xcode.

The two functions that are showing problems are exit and system.

#include <stdio.h>

void main()
{
    FILE *fp;
    char ename[30], ans;
    int t_score;

    fp = fopen("/Users/mmu/Desktop/Program Design/Assignment/Assignment 3/Assignment 3/surveyIn.txt", "a");

    if (fp == 0)
    {
        printf("File could not be opened!\nProgram stopped.\n");
        exit(1);
    }
    else
    {
        do
        {
            fflush(stdin);
            printf("Name: ");
            gets(ename);
            printf("Test mark: ");
            scanf("%d", &t_score);
            fprintf(fp, "%s %d\n", ename, t_score);
            fflush(stdin);
            printf("Do you want to continue (Y/N)? \n");
            scanf("%c", &ans);
        }
        while (ans=='y' || ans=='Y');
    }

    fclose(fp);
    system("pause");
}

The output is this:

Name: warning: this program uses gets(), which is unsafe.
Paul
Test mark: 50
Do you want to continue (Y/N)? 
sh: pause: command not found
Program ended with exit code: 0
Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
paul
  • 1
  • 1
  • 3
    First of all, never *ever* use `gets`. It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function which have been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. Secondly, calling `fflush` on an input-only stream (like `stdin`) is explicitly mentioned as undefined behavior in the C specification. While some compilers add it as a non-portable extension, try to avoid it. – Some programmer dude Oct 01 '19 at 17:07
  • 6
    The function prototypes for `system()` and `exit()` are available: `#include `. – Weather Vane Oct 01 '19 at 17:11
  • 1
    `system("pause");` works only on systems where the command `pause` is available. I'm afraid that your system does not have it. Did you copy this line from a tutorial or such which was written for Windows? – the busybee Oct 01 '19 at 19:17

0 Answers0