Hello Battlecode06,
In your class do this:
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
|
class Words
{
private:
int minlen; //Min length of word
int maxlen; // Max length of word
int count;
string * choices; // Dynamic Array will be used to dispose the additional word choices
/*
**** These forward declarations are in the private section. Should be in the public section
but since all the functions are in the class they are not needed. *****
int count_candidates(ifstream& inFile, const std::string& inFileName); // Function will be used to open txt file of words list
// count_candidates is also used to wordCount all words in file between minlen and maxlen.
// Returns resulting wordCount.
void load_words();
*/
public:
Words(int mn, int mx)
{
minlen = mn;
maxlen = mx;
int count = 0;
}
//string pick_word()
//{
// if (count == 0)
// {
// return string;
// }
//}
//string get_choices()
//{
// return *choices;
//}
~Words()
{
delete[] choices;
}
};
|
Lines 26 - 37 are not needed yet, so do not worry about them yet.
In line 30 you are trying to return a type not a variable. That is your first error.
Your next 3 errors may be in line 61 where you have a space between "std::" and "getline"
In line 40 consider "*choices". With the (*) next to the variable it is easier to tell that you are trying to dereference the variable to get its value, but either way will work.
Maybe I misunderstood what you first wrote or your interpretation of the original directions is a bit off, but I took it as you wanted a class function, or maybe not a class function, to open the file, so in main I did this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int main()
{
//srand(time(NULL)); // needs <ctime> included
srand(static_cast<unsigned int>(time(nullptr)));
int min{}, max{}, response{};
cout << "Enter min: ";
cin >> min;
cout << "Enter max: ";
cin >> max;
Words words(min, max);
std::string inFileName{ "test.txt" };
std::ifstream inFile;
if (response = words.open_file(inFile, inFileName))
return response;
|
and in the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int open_file(ifstream& inFile, const std::string& inFileName)
{
inFile.open(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open.\n";
//std::cout << "\n File \"" << inFileName << "\" did not open.\n";
return 1;
}
return 0;
}
|
For what it is worth I found using "inFile" and "outFile" to work better with less confusion. Easier to keep track of than changing the file stream name from 1 program to the next. It is best to be consistent.
After you have opened the file you just pass it to any function that needs it.
Based on your first class I would start with defining the file stream in "main" then opening the file then print out some part of the array you created in
void count_candidates()
. I changed this to "void" because there is no reason to return anything. The class defines the private variable "count", "wordCount" is more descriptive, so all you have to do is increment this variable that you already have.
I created a simple print function to print the array then went back and add an if statement to use "minlen" and "maxlen" to limit the size of the words printed.
Get this much working before you start into the other functions.
Andy