Resetting data type within a for loop

This mini program is suppose to produce all the possible words that are only one letter away from the string given by the user only seems to produce some of the words. For example if I implement the string "plane" it only give me two possible outcomes, which are "plane" and "alane". But I know "place could be another possible outcome, when thinking about my algorithm. It may be that the datatype within the for loop does not reset itself within the for loop, which is what I thought it would do in the first place but I am not sure.

Code:

/**************************
* COSC 220
***************************
*
* File: main.cpp
*
*
*/

#include <iostream>
#include "console.h"
#include "simpio.h"
#include "strlib.h"
#include <string>
#include "lexicon.h"
#include "sstream"
using namespace std;


int main() {
setConsoleExitProgramOnClose(true);
setConsolePrintExceptions(true);
////////////////////////////////////////////////////////////

Lexicon english("EnglishWords.dat");
string word;
word = getLine("Enter a legal word (must be lowercase): ");

for(int i = 0; i <= word.length(); i++) {
for(char ch = 'a'; ch <= 'z'; ch++) {
string s = "";
s = s + ch;
word.replace(i,1,s);
if (english.contains(word)) {
cout << word << endl;
}
}
}


////////////////////////////////////////////////////////////
getLine("Program finished. Press ENTER to close window(s)");
closeConsoleAndExit();
return 0;
}


Last edited on
It will not reset, because it was declared before the for loop. What you should do is make a copy of it, so you can keep copying it to its original value:

1
2
3
4
string original_word = word; // Creates a copy (do this after getting the user input)

// Then, in the for loop reset like this:
word = original_word;


and that's it! Good luck!
Thank you! Got it working!
Topic archived. No new replies allowed.