Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming pointer issue
    primarykey
    data
    text
    <p>I was assigned to do a project that reads from a file and according to the char it inputs is what it does with the following values so if it reads + it will add the next two numbers or if its an H it'll print out the instructions. Im having trouble with printing the results to another file I pass the pointer to each function as follows <code>void add(int a, int b, FILE *print)</code>, and invoke it as follows.. <code>add(a, b, printer)</code>, it worked perfectly before I tried printing out to a file. The error I get is...</p> <p>in function add:</p> <pre><code>error : incompatible type for argument 1 of fprintf expected 'struct FILE *' but argument is of type 'FILE' </code></pre> <p>this happens too all my functions.</p> <p>here's the code. Thanks !</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void add(int a, int b, FILE *print); void add(int a, int b, FILE *print) { int c; c = a + b; printf("%i + %i = %i\n\n", a, b, c); fprintf(*print, "%i + %i = %i\n\n", a, b, c); } void subtract(int a, int b, FILE *print); void subtract(int a, int b, FILE *print) { int c; c = a - b; printf("%i - %i = %i\n\n", a, b, c); fprintf(*print, "%i - %i = %i\n\n", a, b, c); } void multiply(int a, int b, FILE *print); void multiply(int a, int b, FILE *print) { int c; c = a * b; printf("%i * %i = %i\n\n", a, b, c); fprintf(*print, "%i * %i = %i\n\n", a, b, c); } void divide(int a, int b, FILE *print); void divide(int a, int b, FILE *print) { double c; c = (double)(a/b); printf("%i - %i = %.1lf\n\n", a, b, c); fprintf(*print, "%i - %i = %.1lf\n\n", a, b, c); } void help(FILE *print); void help(FILE *print) { printf("+ i j [Integer Add]\t\tAdds integers i and j and print out result\n\n"); printf("* i j [Integer Multiply]\tMultiply integers i and j and print out result\n\n"); printf("- i j [Integer Subtract ]\tSubtract integer j from i and print out result\n\n"); printf("/ i j [Integer Divide ]\t\tDivide integer i by j and print out result of integer division\n\n"); printf("H [Help ]\t\t\tPrint a short synopsis of all the available commands\n\n"); printf("Q [Quit ]\t\t\tQuit\n\n"); printf("\n\n"); fprintf(*print, "+ i j [Integer Add]\t\tAdds integers i and j and print out result\n\n"); fprintf(*print, "* i j [Integer Multiply]\tMultiply integers i and j and print out result\n\n"); fprintf(*print, "- i j [Integer Subtract ]\tSubtract integer j from i and print out result\n\n"); fprintf(*print, "/ i j [Integer Divide ]\t\tDivide integer i by j and print out result of integer division\n\n"); fprintf(*print, "H [Help ]\t\t\tPrint a short synopsis of all the available commands\n\n"); fprintf(*print, "Q [Quit ]\t\t\tQuit\n\n"); fprintf(*print, "\n\n"); } char quit(void); char quit(void) { char user; printf("Do you really want to quit?(y/n): "); scanf(" %c", &amp;user); return user; } int main() { char trigger; char user; char fname; char lname; int a; int b; FILE *myPointer; FILE *printer; myPointer = fopen("CommandsProj1.dat", "r"); printer = fopen("results.dat", "w"); if (myPointer == NULL) { printf("error opening the file"); fprintf(printer, "error opening the file"); } else { fscanf(myPointer, " %c%c", &amp;fname, &amp;lname); printf("Initials %c%c\n\n\n", fname, lname); while(user != 'y') { fscanf(myPointer, " %c", &amp;trigger); switch(trigger) { case '+': fscanf(myPointer, "%i%i", &amp;a,&amp;b); add(a,b,myPointer); break; case '-': fscanf(myPointer, "%i%i", &amp;a, &amp;b); subtract(a,b,myPointer); break; case '/': fscanf(myPointer, "%i%i", &amp;a,&amp;b); divide(a,b,myPointer); break; case '*': fscanf(myPointer, "%i%i", &amp;a,&amp;b); multiply(a,b,myPointer); break; case 'H': help(myPointer); break; case 'Q': user = quit(); } } } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload