I'm trying to get my program to read from a file a list of words into the original member and the sorted version of the word into the sorted member of each element of the array. Can someone give me advice on how to approach this?
#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
}
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;
}
Your program appears to be a template file provided to you by your teachere. The comments in the program give you the steps required at each stage.
Best approach is to look at the comments in turn and start writing some code for each one. Like Handy Andy says I'd start at line 23. Then line 23. Then you're done by the look of it.
Do it in simple steps with lots of testing before you write more than 1 or 2 lines of code.
Get back if you get stuck with any of your code. Good luck with it. :)