Sublime Forum

ERROR: 'sleep' was not declared in this scope

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

int main()
{
	int p;
	int ai;
	
/*	int '0' = "Rock";
	int '1' = "Paper";
	int '2' = "Scissors";*/
	
	printf("\n\t\t\tROCK PAPER SCISSORS GAME\n\n");
	printf("Enter a number (0 for Rock, 1 for Paper, 2 for Scissors): ");
	scanf("%d", &p);
	srand ( time(NULL) );
	ai = rand()%3;
	switch(p)
	{
		case 0: printf("\n\t\t\tYou've chosen Rock\n");
				sleep(2);
				printf("\n\t\t\tCOMP have chosen %d\n", ai);
				if(ai==0)
				{
					printf("\n%36s","DRAW!!");break;
				}else if(ai==2)
				{
					printf("\n%38s","You Win!");break;
				}else if(ai==1){
					printf("\n%37s","You Lose!");break;
				}
		case 1: printf("\n\t\t\tYou've chosen Paper\n");
				sleep(2);
				printf("\n\t\t\tCOMP have chosen %d\n", ai);
				if(ai==1)
				{
					printf("\n%36s","DRAW!!");break;
				}else if(ai==0)
				{
					printf("\n%38s","You Win!");break;
				}else if(ai==2){
					printf("\n%37s","You Lose!");break;
				}
		case 2: printf("\n\t\t\tYou've chosen Scissors\n");
				sleep(2);
				printf("\n\t\t\tCOMP have chosen %d\n", ai);
				if(ai==2)
				{
					printf("\n%36s","DRAW!!");break;
				}else if(ai==1)
				{
					printf("\n%38s","You Win!");break;
				}else if(ai==0){
					printf("\n%37s","You Lose!");break;
				}
	}
}

does anyone know how to fix this?..it’s a Rock Paper Scissors game that I made…by the way…im using windows…and I’m using sublime text 3 to compile it…

EDIT : it is solved by adding #include <windows.h> and capitalizing the “s” in “sleep”

0 Likes

#2

I’d say you’re getting that error message becuase ‘sleep’ isn’t declared in that scope.

You’re welcome.

1 Like

#3

It compiles and runs fine for me:

tmartin:dart:~> gcc rps.c -o rps
tmartin:dart:~> rps

                        ROCK PAPER SCISSORS GAME

Enter a number (0 for Rock, 1 for Paper, 2 for Scissors): 0

                        You've chosen Rock

                        COMP have chosen 0

                              DRAW!!tmartin:dart:~> 

Compiling with gcc -Wall rps.c -o rps throws a warning because you’ve specified that main returns an integer but you don’t have a return value at the end, but that’s neither here nor there.

Based on your error message I would assume that your compiler needs more than just including time.h in order to get the time function. You don’t mention what compiler you’re using though, so I can’t say for sure.

0 Likes

#4

I’m guessing you finally broke down and typed “where is sleep declared” into google and clicked the first result:

Amiright?

0 Likes