Words File

Header.h:

#pragma once
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
#include<string>

using namespace std;
const int words = 2311;

void initWords(string[]);
void findWords(string[], char);
int removeWord(string[], char, int, int, bool);

main.cpp:

#include "Header.h"

int main()
{

	string guess;
	int n;
	int numWordsLeft = words;
	char c;
	char letOne;
	char letTwo;
	char letThree;
	char letFour;
	char letFive;
	int letOneVal;
	int letTwoVal;
	int letThreeVal;
	int letFourVal;
	int letFiveVal;
	int choice;

	string fiveWords[words];

	initWords(fiveWords);

	gradeWords(fiveWords, numWordsLeft);

	for (int h = 0; h < 6; h++)
	{

		cout << "Enter your guess: ";
		cin >> guess;

		for (int i = 0; i < guess.size(); i++)
		{

			switch (i)
			{
			case 0:
				letOne = guess[i];
				break;
			case 1:
				letTwo = guess[i];
				break;
			case 2:
				letThree = guess[i];
				break;
			case 3:
				letFour = guess[i];
				break;
			case 4:
				letFive = guess[i];
				break;
			default:
				cout << "Error guess length! Words can only be five letters long" << endl;
			}

		}

		for (int i = 1; i < 6; i++)
		{

			cout << "Was letter " << i << " green(1), yellow(2), or grey(3)? ";
			cin >> choice;

			switch (choice)
			{
			case 1:
				c = guess[i - 1];
				n = removeWord(fiveWords, c, 0, 5, false);
				numWordsLeft -= n;
				n = removeWord(fiveWords, c, i - 1, i, false);
				numWordsLeft -= n;
				break;
			case 2:
				c = guess[i - 1];
				n = removeWord(fiveWords, c, i - 1, i, true);
				numWordsLeft -= n;
				n = removeWord(fiveWords, c, 0, 5, false);
				numWordsLeft -= n;
				break;
			case 3:
				c = guess[i - 1];
				n = removeWord(fiveWords, c, 0, 5, true);
				numWordsLeft -= n;
				break;
			}

			switch (i)
			{
			case 1:
				letOneVal = choice;
				break;
			case 2:
				letTwoVal = choice;
				break;
			case 3:
				letThreeVal = choice;
				break;
			case 4:
				letFourVal = choice;
				break;
			case 5:
				letFiveVal = choice;
				break;
			}

		}

		if (numWordsLeft == 1)
		{

			string word;
			int i = 0;

			while (true)
			{

				bool n = false;

				if (fiveWords[i] != "\0")
				{

					word = fiveWords[i];
					n = true;

				}

				if (n) { break; }

				i++;

			}

			cout << endl << "The answer is " << word << endl;
			cout << "Enter any number to continue: ";
			cin >> i;

			break;

		}
		else
		{

			int i = 0;
			while (true)
			{

				bool n = false;

				if (fiveWords[i] != "\0")
				{

					cout << fiveWords[i] << endl;
					n = true;

				}

				if (n) { break; }

				i++;

			}

			cout << endl << numWordsLeft << endl << endl;

		}

	}

}

findWords.cpp:

#include "Header.h"

void findWords(string fiveWords[], char let)
{

	char c;
	int count = 0;

	for (int i = 0; i < 1858; i++)
	{

		for (int j = 4; j < 5; j++)
		{

			c = fiveWords[i][j];

			if (c == let)
			{

				cout << fiveWords[i] << endl;
				count++;
				break;

			}

		}

	}

	cout << count << endl;

}

removeWord.cpp:

#include "Header.h"

int removeWord(string fiveWords[], char c, int start, int stop, bool contains)
{

	int n = 0;
	bool con;

	for (int i = 0; i < words; i++)
	{

		con = false;

		if (fiveWords[i] != "\0")
		{

			if (contains)
			{

				for (int j = start; j < stop; j++)
				{

					if (fiveWords[i][j] == c)
					{

						fiveWords[i] = "\0";
						n++;
						break;

					}

				}

			}
			else
			{

				for (int j = start; j < stop; j++)
				{

					if (fiveWords[i][j] == c)
					{

						con = true;

					}

				}

				if (!con)
				{

					fiveWords[i] = "\0";
					n++;

				}

			}

		}

	}

	return n;

}

initWords.cpp:

#include"Header.h"

void initWords(string fiveWords[])
{

	fstream newfile;
	newfile.open("fivewords.txt", ios::in);
	int i = 0;

	if (newfile.is_open())
	{

		while (getline(newfile, fiveWords[i]))
		{

			i++;

		}

		newfile.close();

	}
	else
	{

		cout << "File failed to open!";

	}

}
Cal Poly Pomona Engineering Logo