argument of type "void" is incompatible with parameter of type "FILE *__restrict__"

  c++, file, void

I am building rock paper scissors in the C/C++ programming language. I created a void function that stores the score of the program. Now I want to give it to a file. The full program:

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

void rockPaperScissors(char, char);

int main()
{
    char you, comp;
    srand(time(0));
    int number = rand() % 3 + 1;
    printf("Enter the users turnn");
    scanf("%c", &you);

    if (number == 1)
    {
        comp = 'r';
    }
    if (number == 2)
    {
        comp = 'p';
    }
    if (number == 3)
    {
        comp = 's';
    }

    rockPaperScissors(you, comp);
    printf("You chosed %c & computer chosed %cn", you, comp);
    FILE *ptr;
    ptr = fopen("results.txt", "a");
    fprintf(rockPaperScissors(you, comp), ptr);
    fclose(ptr);

    return 0;
}

void rock paper scissors(char you, char comp)
{
    if ((you == 'r' && comp == 's') || (you == 'p' && comp == 'r') || (you == 's' && comp == 'p'))
    {
        printf("You won!n");
    }
    else if ((comp == 'r' && you == 's') || (comp == 'p' && you == 'r') || (comp == 's' && you == 'p'))
    {
        printf("Computer won...n");
    }
    else if (comp == you)
    {
        printf("Game is grawn");
    }
    else
    {
        printf("Chose something validn");
    }
}

Here I am thinking to use fprintf with a void function, but I am unable to do it, here is what I am doing

FILE *ptr;
ptr = fopen("results.txt", "a");
fprintf(rockPaperScissors(you, comp), ptr);
fclose(ptr);

Here is the full error I am coming with

main.c: In function ‘main’:
main.c:32:13: error: invalid use of void expression
   32 |     fprintf(rockPaperScissors(you, comp), ptr);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:32:43: warning: passing argument 2 of ‘fprintf’ from incompatible pointer type [-Wincompatible-pointer-types]
   32 |     fprintf(rockPaperScissors(you, comp), ptr);
      |                                           ^~~
      |                                           |
      |                                           FILE *
In file included from main.c:1:
/usr/include/stdio.h:327:44: note: expected ‘const char * restrict’ but argument is of type ‘FILE *’
  327 |                     const char *__restrict __format, ...);
      |                     ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

Thanks in advance!

Source: Windows Questions C++

LEAVE A COMMENT