The cause of the issue was when I inserted this code:
1 2
infile >> wordlist[i];
i++;
I'm not sure why my the code will execute. Additionally, it says "no match for operator >> in infile...etc" could someone give me a clue as to what to do? I noticed changing >> to << solved a bunch of issues but I still get the no match for operator issue.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
string sort(string s); // returns alphabetized version of s
struct word {
string sorted; // "ackrt"
string original; // "track"
};
int main() {
constint maxSize = 200000;
static word wordlist[maxSize]; // to hold the words
int numWords; // total number of words read in from file
ifstream infile;
infile.open("words.txt");
int i = 0;
while (infile && i < maxSize) {
// read each word into wordlist until the end of file
// your code here
infile >> wordlist[i];
i++;
}
numWords = i;
string w;
do {
bool found = false;
cout << "Please type in a word to unscramble or 'q' to quit: ";
// find each occurrence of wordslist[i].sorted that matches sort(w)
// your code here.
} while (w != "q");
return 0;
}
string sort(string s) {
// return a string with the characters of s rearranged alphabetically.
// For example sort("track") returns "ackrt"
string t;
while (s != "") {
int minIndex = 0;
for (int i = 1; i< s.length(); i++)
if (s[i] < s[minIndex])
minIndex = i;
t += s[minIndex]; // add the smallest character to t
s.erase(minIndex,1); // remove the smallest character from s
}
return t;
}
ere.
infile >> wordlist[i];
wordlist[i] is of type word. This is a type that your program created and hence something the standard library does not know how to operate on unless you tell it. I suspect that you meant infile >> wordlist[i].original;
because the compiler does know what to do with >> and strings.
Unless you put some sort of input (i.e. use cin >>) into your loop
1 2 3 4 5 6 7
do {
bool found = false;
cout << "Please type in a word to unscramble or 'q' to quit: ";
// find each occurrence of wordslist[i].sorted that matches sort(w)
// your code here.
} while (w != "q");