I have my code working properly for one word but when I added in the if else statements at the start and another switch statement i am getting a few errors. Any help would be appreciated :)
here is the code:
#include<stdio.h>
#include<stdlib.h>
#define letters 6
#define letters2 9
main()
{
srand((unsigned)time(NULL));
int num = rand() %5 +1 ;
if(num == 1)
{
char word[letters] = {'f','l', 'i', 'c', 'k'};
char spaces[letters] = {'-', '-', '-', '-', '-'};
}
else
if(num == 2)
{
char word2[letters] = {'s','p', 'o', 'r', 't'};
char spaces[letters] = {'-', '-', '-', '-', '-'};
}
else
if(num == 3)
{
char word3[letters] = {'b','o', 'o', 't', 's'};
char spaces[letters] = {'-', '-', '-', '-', '-'};
}
else
if(num == 4)
{
char word4[letters2] = {'f','o', 'o', 't', 'b', 'a', 'l', 'l'};
char spaces2[letters2] = {'-', '-', '-', '-', '-', '-', '-', '-'};
}
else
if(num == 5 )
{
char word4[letters2] = {'s','w', 'i', 'm', 'm', 'i', 'n', 'g'};
char spaces2[letters2] = {'-', '-', '-', '-', '-', '-', '-', '-'};
}
int startmenu, PlayGame, ExitGame;
int lives = 6;
int i;
int j = 0;
int exit;
int correct;
char symb;
int length = 5;
printf("Lets PLay Hangman!");
printf("\n1. Play game\n0. Exit");
scanf("%d",&startmenu);
switch(num)
{
case 1:
{
switch( startmenu )
{
case 1:
{
while ((lives>0) && (j < length))
{
correct = 0;
printf("\nPlease enter a letter: ");
do
{
scanf("%c", &symb);
if((symb > 32) && ((symb < 97) || (symb > 122)))
{
printf("\nInvalid character\nEnter a letter: ");
}
}while((symb < 97) || (symb > 122));
for(i=0;i < length;i++)
{
if(symb == word[i])
{
spaces[i] = symb;
correct = 1;
printf("\nThat was correct! you have %i lives left\n",lives);
printf("\nYou have %s\n",spaces);
if(word[1]==spaces[1]&&
word[2]==spaces[2]&&
word[3]==spaces[3]&&
word[4]==spaces[4]&&
word[5]==spaces[5])
{
printf("\nCongtratulations You win :)");
printf("Please press 0 to exit");
scanf("%d",&exit);
switch (exit)
{
return 0;
}
}
}
}
if(correct == 0)
{
lives--;
if(lives > 0)
{
printf("\nSorry that was not right! %i lives left\n",lives);
}
else
{
printf("\nSorry You lost! :(\n");
printf("Please press 0 to exit");
scanf("%d",&exit);
switch (exit)
{
{
return 0;
}
}
}
printf("\nYou have %s\n",spaces);
}
}
}
case 0:
{
return 0;
}
}
getchar();
getchar();
{
case 2:
switch( startmenu )
{
case 1:
{
while ((lives>0) && (j < length))
{
correct = 0;
printf("\nPlease enter a letter: ");
do
{
scanf("%c", &symb);
if((symb > 32) && ((symb < 97) || (symb > 122)))
{
printf("\nInvalid character\nEnter a letter: ");
}
}while((symb < 97) || (symb > 122));
for(i=0;i < length;i++)
{
if(symb == word2[i])
{
spaces[i] = symb;
correct = 1;
printf("\nThat was correct! you have %i lives left\n",lives);
printf("\nYou have %s\n",spaces);
if(word2[1]==spaces[1]&&
word2[2]==spaces[2]&&
word2[3]==spaces[3]&&
word2[4]==spaces[4]&&
word2[5]==spaces[5])
{
printf("\nCongtratulations You win :)");
printf("Please press 0 to exit");
scanf("%d",&exit);
switch (exit)
{
return 0;
}
}
}
}
if(correct == 0)
{
lives--;
if(lives > 0)
{
printf("\nSorry that was not right! %i lives left\n",lives);
}
else
{
printf("\nSorry You lost! :(\n");
printf("Please press 0 to exit");
scanf("%d",&exit);
switch (exit)
{
{
return 0;
}
}
}
printf("\nYou have %s\n",spaces);
}
}
}
case 0:
{
return 0;
}
}
getchar();
getchar();
}
}