Hello Everyone,
I am a new member in the forum. I had a question regarding a code. The code is not able to open the input file. Any help is appreciated.
thanks
Here is the code,
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int MAX_TRY = 8;
const int WORDS_NUMBER = 2; // why wrod number??
const int MAX_SIZE = 14;
//int point_earn = 0;
ifstream infile; // stream input file variable declaration globally
void initialize(char [],int);
void readString(char [], int &);
//void guess(char []);
//void printSolution(char []);
//void compareWord(char []);
int main()
{
char correct_word[MAX_SIZE] ;
char solution[MAX_SIZE];
char letter;
int length;
cout << " WELCEOME TO THE HANGMAN Game " << endl;
cout << " You will be given up to " << MAX_TRY << " attempts to find a letter." << endl;
cout << " You will get a chance to solve the word after each letter attempt.\n\n\n";
infile.open("Proj4_input.txt"); //input file opens
if(!infile) //checks if input file can be read properly
{
cout << "Cannot open the file\n"; //if error in inputfile invokes to output this
exit(1);
}
readString(correct_word, length);
initialize(solution,length);
cout << "The word is " << correct_word << endl;
cout << "THe solution is " << solution<< endl;
cout << "Enter a letter of your choice : ";
cin >> letter;
//guess(correct_word);
//printSolution(correct_word);
//compareWord(correct_word);
infile.close(); //input file closed
return 0;
}
void initialize(char guessed_word[MAX_SIZE],int length)//initialize SOLUTION to *'s and other global variables to 0
{
int j;
for(j = 0; j < length; j++)
{
guessed_word[j] = '*';
}
for(j=length; j< MAX_SIZE;j++)
guessed_word[j] = ' ';
cout << guessed_word << endl;
}
void readString(char word[], int& length)// read each word from the input file ????
{
length = 0;
infile.getline(word,MAX_SIZE);
length = strlen(word);
}
/*
void guess(char solution[MAX_SIZE])//Read in guesses until the limit is reached or the word is guessed,display the partial solution to the user
{
int index;
int j = 0;
int wrong_guess = 0;
char guess_letter;
cout << "Enter the guessed letter : ";
cin >> guess_letter;
for(index = 0; index < MAX_SIZE; index++)
{
if (guess_letter == solution[index])
{
j++;
cout << "The letter " << guess_letter << "in the word presents " << j << "times\n";
cout << "The partial solution is " << solution <<endl;
}
else
{
cout << " Uff, Wrong guess!! Try again : ";
while(wrong_guess < MAX_TRY)
{
wrong_guess = wrong_guess++;
cout << "your wrong guess is " << wrong_guess << endl;
}
cin >> guess_letter;
}
}
}
void printSolution(char word_guessed[MAX_SIZE])//print the guessed word and the number of times the player has tried
{
int count;
for(int k = 0; k < MAX_SIZE; k++)
{
cout << word_guessed[k] << endl;
}
for(count = 0; count < MAX_TRY; count++)
cout <<"you have tried " << count << "times\n";
}
void compareWord(char word_original[MAX_SIZE]) //It is a function. It returns TRUE if every element of SOLUTION is not a '*'
{
char word_guessed[MAX_SIZE];
if(strcmp(word_original, word_original) == 0)
{
cout << "Wrong!!! YOU HAVE LOST THIS ROUND!\n";
cout << " The original word was " << word_original << endl;
}
else
{
cout << "Congrats!!! YOU HAVE WON THIS ROUND!\n";
cout << "The word is " << word_original << endl;
}
}
*/