Porting code from C++ to C

Im trying to port code from C++ to C but I cant seem to figure out whats my errors

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

typedef int bool;
#define true 1
#define false 0
//Hangman Game

string LoadFile() //To load the file
{
string W; //Declare Varibles
string words[20];
int count = 0;

ifstream txtfile; //the in file

txtfile.open("words.txt"); //opens file I lost the file so insert the file in here
if (!txtfile || !txtfile.is_open()) {
	return "";
}

for (count=0; !txtfile.eof(); count++)
{
	getline(txtfile,W);
	words[count] = W;
}
int k=(rand()%count); //Picks a random word from file.
txtfile.close(); //closes file

return words[k];
}	


void RunGame(string word) //Runs the game
{
int k;
int wrong = 0;
char guess;	
char letters[20];
char x;
char incorrect[20];
char correct[20];


for (k=0; k<word.length();k++) //Blanks for each letter of your word
{
	letters+= '_';
}

int count = 0;
for (x=0; x<15; x++) //15 Guesses
{
	printf("Guess a letter");
	scanf("%s", guess);
		
	bool found = false;
	for (k=0; k<word.length();k++) //Compare you guess to the word
	{
		if (guess == word[k])
		{
			printf("Guess is correct");
			letters[k] = guess;
			found = true;
			count++;	
		}
	}
	if(found)
		correct += guess;
	else
	   incorrect += guess;

	if(count == word.length())
	{
		printf("You Win! Game Over");
		break;
	}


	if (!found) //You lose when you get 6 wrong letters
	{
		printf("Wrong letter guessed, try again");
		
		wrong++;
		if (wrong==6)
		{
			printf("Too many incorrect guesses, Game Over. You lose");
			printf("The word was %s\n", word);
			break;
		}
	}
	
	printf("Word: %s\n", letters);
	printf("Correct: %s\n", correct);
	printf("Incorrect: %s\n", incorrect);
	
	
    }
}



int main() //main
{
printf("Hangman\n");

srand(time(NULL));

char playagain = 'Y';

while (playagain == 'Y')
{
	char word[50] = LoadFile();
	if (word == "") {
		printf("Unable to load file\n");
		return -1;
	}
		
	RunGame(word);
	while (true) {
		printf("Play Again? (Y/N) ");
		scanf("%s" , playagain);
		playagain = toupper(playagain);
		if (playagain == 'Y' || playagain == 'N')
			break;
		printf("Incorrect input.");
	}
}

printf("Hope you enjoyed the game\n");
return 0;
}

What am I missing, I tried changing and running a clean build but I still get errors with the CMATH class even though I took it out

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.