possible array function question...

I have a program that I am writing in c++ for a class project. SEE BELOW
my problem is it is not giving me a chance to get the word correct even when it is correct. So I know it has something to do with passing alpha, and we haven't used vectors so I have to use arrays. My professor said something about passing alpha by reference. But not sure where to do it...I tried a few places but it didn't work. Any idea?? Thanks-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
this is a memory game that you can play with repetition it will ask you
to list 10 items in alphabetical order,
then you will need to recall what those items were in random order
since you gave the items you think it will be easy, but it may not be!!  */
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 10;
string itemToPack;

char showLetters(int SIZE);
void testWords(char);
void testWords(string, int&);

int main()
{
	string yourWord;
	string q = "q";
	cout << "\t\t\tWELCOME!\n \t\tTo The Alphabet Memory Game \n\nYou will be asked"
		<< " for 10 items in alphabetical order a-j. \n"
		<< "For instance, alligator. Return. Ball. Return.\n"
		<< "All the way to J. (hit return after each word) \n"
		<< "After you have entered the words your screen will go blank\n";
	cout << "Then you will be asked what you entered for the\n"
		<< "various letters of the alphabet. They may not be in order!";
	cout << "\n\nEnter your 10 words in alphabetical order now ___.\n";
	cout << "(Remember type 10 alphabetic words in order of the alphabet!)\n ";
	//getting the ten words to remember
	string words[SIZE];
	for (string &itemToPack : words)
	{
		getline(cin, itemToPack);
	}
	//prompting user to retain the words
	cout << "\t\tRemember your words!\n";
	//words written to a text file
	ofstream outputFile("WordsInGame.txt");
	for (string val : words)
	{
		outputFile << val << endl;
	}
	cout << "When ready press any key and return, the information will disapear!\n";
	cout << "\nwhen you get the blank screen wait to get more instructions.";
		char anyLetter;
		cin >> anyLetter;

	//Words vanish from the user screen but not from the file
	/***************************************************
	setting up use of cmath function to clear the screen
	***************************************************/
	double y = exp(10);
	//use of cmath function to clear the screen
	cout << string (y,'\n');
	cout << "\t\t\t     Now it's time \n\n\t\t\t to test your memory! \n\n\t\t\tWhat was your words???\n\n";
	cout << "(if you want to quit press 'q' when you are prompted to enter something)\n\n";
	while (yourWord != q)
	{
		char choice;
		int alpha = SIZE;
		cout << "\nWhat did you put for  " << showLetters(alpha) << " ";
		cin >> yourWord;
		testWords(yourWord, alpha);
		cout << "\nDo you want a hint?\n";
		cout << "Enter y for yes or n for no and q for quit. \n";
		cin >> choice;
		if (choice != 'q');
		testWords(choice);
	}
	cout << "\n\nThanks for playing!\n\n";
	system("pause");
	return 0;
}
/********************************************************************
This function gives us a random letter 
from a to j to prompt the user for a word
*********************************************************************/
char showLetters(int alpha)
{
	const int MIN_VALUE = 0;
	const int MAX_VALUE = SIZE;
	unsigned seed = time(0);
	srand(seed);
	char alphabet[SIZE] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
	for (int count = 0; count < SIZE; count++)
		alpha = (rand() % (MAX_VALUE - MIN_VALUE)) + MIN_VALUE;
	return alphabet[alpha];
}
/************************************************************************
this is testing if the word entered is the same word from the text file
it also is a overloaded function
*************************************************************************/
void testWords(string yourWord, int& alpha)
{
	ifstream inputFile("WordsInGame.txt");
	if (inputFile.is_open())
	{
		string myArray[SIZE];
		for (int i = 0; i < SIZE; i++)
		{
			inputFile >> myArray[i];
		}
		if (myArray[alpha] == yourWord)
		{
			cout << "You are correct!";
		}
		else
			cout << "I'm sorry you are wrong- game over\n\n";
			cout << "\nThe original word was " << myArray[alpha];
	}
}
/******************************************************
this is for my overloaded function!!
*******************************************************/
void testWords(char choice)
{
	switch (choice)
	{
	case 'y':
		cout << "Really, you can't recall 10 words you chose? You need to play this more!";
			break;
	case 'n':
		cout << "Ok - great!  What confidence!  Keep going!!";
		break;
	case 'q':
		cout << "\n\nThanks for playing!\n\n";
		system("pause");
		exit(0);
	default:
		cout << "you did not enter y, or n, please choose y for yes or n for no.";
	}
}
Last edited on
A debug tip: print alpha, myArray[alpha] and yourWord right before the comparison.


Code posting tip: use code tags.
Last edited on
sorry now I have code tags, I read a little about how to use this site but I guess I missed that, thanks
did the printing to see my code, I got my word entered and 10...not sure how to get what I really wanted which was a comparison of my word and what was previously entered...
ok after looking around some more on references and the '&' I figured it out...
line 19 I changed to:
char showLetters(int&);

and line 87 to
char showLetters(int& alpha)
and it works THANKS!!!
Topic archived. No new replies allowed.