How to get unscramble program to read a file

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?

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string sort(string s); // returns alphabetized version of s

struct word {
	string sorted;		// "ackrt"
	string original;	// "track"
};

int main() {
	const int 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;
}	

Last edited on
Hello andygarc1a,

You can start with these links:

http://www.cplusplus.com/reference/fstream/fstream/
http://www.cplusplus.com/doc/tutorial/files/

Line 23 is where you will need code to read the file.

Come up with some code and post it and describe your problem to move forward.

Andy
closed account (48T7M4Gy)
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. :)
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/204293/
Topic archived. No new replies allowed.