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.
you did not declare the function
computerPlay()
check after declaring this function.
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 :