I'm creating a Wheel of Fortune program, and I have just started. Currently I'm just trying to take a word from a file and transfer it into an array, and with that create an array with the same size, but full of asterisks (*'s). My class is making me use certain function prototypes, which makes it so that I can't change them around and I'm stuck trying to make an array inside of a function. Here is my code:

/* GLOBAL HEADERS */

#include <iostream>                             // include standard I/O library
#include <iomanip>                              // include IO manipulators
#include <fstream>                              // include file streaming
#include <string>                               // include C++ string class
#include <cctype>                               // for the isalpha function
#include <ctime>								// to access the computer's clock
#include <cstdlib>								// needed for rand() and RAND_MAX
using namespace std;                            // access standard namespace

/* =========================================================================*/
/* GLOBAL NAMED CONSTANTS */
const int WHEEL_SIZE = 15;				//Number of entries in the wheel,
const int PHRASE_SIZE = 81;				// int [optional if you use only
										// string class objects and no Cstrings
										// or array of char]
const int NUM_PLAYERS = 3;               // Number of players in game
const int BANKRUPT = 0;					// Entry on the wheel that signifies bankrupt
const int LOSE_TURN = -1;				// Entry on the wheel that signifies lose a turn
const int VOWEL_COST = 250;				//Cost to buy a vowel

const int wheel[WHEEL_SIZE] =
{50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 1000, 1500,
2500, BANKRUPT, LOSE_TURN};
/*==========================================================================*/
/* GLOBAL VARIABLES */

/*==========================================================================*/
/* GLOBAL FUNCTION PROTOTYPES */
void Open_Data_File (ifstream&);
int initialize_arrays(string&, string&);
/*==========================================================================*/

/* MAIN FUNCTION */

int main ()
{
	 string phrase;			// to store the phrase array
	 string puzzle;			// to store the puzzle * array

	initialize_arrays(phrase, puzzle); //opens the initialize_arrays function

	return (0);
}

/*==========================================================================*/
int initialize_arrays(string& phrase, string& puzzle)
/*
This function should ask for the input file, open the input file, read the
character string into the phrase array and place the appropriate *s in the
puzzle array. For each alphabetic character in the phrase array, a respective
* belongs in the puzzle array. If the character is not an alphabetic character
(i.e., ‘-’, ‘!’, etc.) then the character is copied into the puzzle array.
You must do the usual error checking, i.e., if the file name is invalid,
loop until you obtain a valid file name from the user*/

{
	string filename;		// name of file as input by user
	ifstream inFile;
	int count = 0;


	// ask for file name and try to open
	cout << "Enter a file name which cannot contain blanks: ";
	cin >> filename;
	inFile.open (filename.c_str());

	// error handling loop: if file does not open, ask user again until it does
	while (!inFile)
	{
		inFile.clear();
		cout << "Please try agian. Enter a file name: ";
		cin >> filename;
		inFile.open(filename.c_str());
	}

	for (count = 0; count < PHRASE_SIZE; count++)
		inFile >> phrase[count];

	// Close the file.
	inFile.close();

	return(count);

I havn't gotten everything done yet, but I cant seem to figure out a way to actually create a "phrase" array without it giving me an error message about mixing the string& with a phrase[PHRASE_SIZE] but I also cant change the function to accept phrase as a phrase originally.

I'm not really sure what your trying to accomplish here. Your for loop is incorrect because what if the file doesn't contain 81 characters? What if the file has a string with more than 81 characters? When checking for an EOF, consider looping through with a while statement like

while(!inFile.eof())

. Now as far as your code logic is concerned, why are you returning the count of characters? A string object has a built in method to tell you how big the string is. I know that you cannot change the function prototypes so I would think (Hopefully, as long as your teacher is worthy of teaching) would want the int returned to tell your program whether the function call was successful or not.

Sorry that return wasn't supposed to be there, the int return is just for returning how many characters are in the file, which I will get to later. I found i was supposed to change the "inFile >> phrase[count];" to "inFile >> phrase;" but now I am getting an actual error message saying the "string subscript is out of range".

For instance I have a text file with the text "sean" in it. The word sean does get set to string, but when tryin to print out the word using a for loop

"
for (count = 0; count < PHRASE_SIZE; count++)
cout << phrase[count];
"

after the word sean is displayed i get the "string subscript is out of range" message.

any thoughts?

I declared an int length = phrase.length(); and changed the for loop to
"
for (count =0; count < length; count++
cout << phrase[count];
"

this seems to have fixed it!

Thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.