Function not returning char correctly: error during compile

160 views Asked by At

I am currently trying to write a simple Rock, Paper, Scissors program in C. The entire point of the assignment is to get familiar with using chars. Here is the code I currently have. It is incomplete, as I am stuck.

#include <stdio.h>

int main()
{
    int pScore = 0;
    int cScore = 0;
    int ties = 0;
    char play, go;
    char comp = 'r';

    printf("Welcome to Rock-Paper-Scissors!\n");
    printScore(pScore, cScore, ties);
    for ( ; ;)
    {
        printf("Do you want to play Rock-Paper-Scissors? (y/n): ");
        scanf("\n%c", &go);
        if(go == 'y' || go == 'Y')
        {
            printf("Enter your choice (r,p,s): ");
            scanf("\n%c", &play);
            comp = computerPlay();
            printf("Computer plays: %c", comp);
        }
        else
            break;
    }
    printf("\nThanks for Playing!!\n\n");
}

char computerPlay()
{
    int r = rand() % 3;
    char c;
    if (r == 0)
    {
        c = 'r';
    }
    else if (r == 1)
    {
        c = 'p';
    }
    else if (r == 2)
    {
        c = 's';
    }
    return c;
}

int printScore(int p, int c, int t)
{
    printf("\nHuman Wins: %d       Computer Wins: %d       Ties: %d\n\n", p, c, t);
    return 0;
}

The compiler is giving me the following error:

RPS.c:35:6: error: conflicting types for ‘computerPlay’

RPS.c:28:14: note: previous implicit declaration of ‘computerPlay’ was here

It seems to me this should be working just fine.... I'm at a loss.

3

There are 3 answers

2
Gangadhar On

you did not declare the function computerPlay()

check after declaring this function.

char computerPlay(void);   

add this statement after #include<stdio.h>

EDIT:

All identifiers in C need to be declared before they are used. This is true for functions as well as variables. For functions the declaration needs to be before the first call of the function. A full declaration includes the return type and the number and type of the arguments

simple example :

int sum (int, int);    // declared a function with the name sum 

result =sum(10,20);    // function call  

int sum (int a, int b) // defined a function called sum 
    {
        return a + b;
    }
0
Chowlett On

At the point the compiler encounters a use of a function, it needs to know about the existence of the function.

In your code, when the compiler (reading down your source file) meets comp = computerPlay();, it hasn't seen anything telling it that function exists. It has a guess at what the function is, and guesses int computerPlay(void) - by default, C will always assume a function returns int. Then it encounters the actual definition later on, which conflicts with the guess it already made - the function returns char, not int.

The solution is to make the compiler aware the function exists before you use it. You can either:

  • move the entire function definition above main
  • put a declaration static char computerPlay(void); above main, indicating it's available to this source file only
  • put a declaration char computerPlay(void); into a separate header file, and #include that file above main, thereby making the function available to other source files (given other steps beyond the scope of this question)
0
Nirav Kamani On

You are using two functions one is

char computerPlay() and second one is

int printScore(int p, int c, int t) both of these first must have to declared below the include statement.

So just make the following statement below the include statement.

char computerPlay();

int printScore(int , int , int );

Second thing you are also using int main() so at the end you must have to return integer so you also have to add following statement at the end of the main return 0 or any other integer.