lundi 22 avril 2019

C++ anagram string from readfile not working

I'm creating an anagram program where the user enters a word and the program searches a csv file to find an anagram of the word that may be in the file. The way I'm trying to go about this is to order both the current string(currentString) in the csv file and the input word(localString) alphabetically, then I see if the two alphabetically ordered words are the same. If they are, it's an anagram. For some reason, I can't work out why it's not displaying the anagram even though there are anagrams of the word in the csv file. Here's a section of the code below:

void FindAnagrams(string originalString){
    string localString = originalString; //take a copy of the original string as provided by the user 

    string currentWord; 
    ifstream dbFile; 
    dbFile.open(cDATAFILE); 

    while(!dbFile.eof()) { //this sets up a loop that repeats until we have read every line in the opened file; line by line.

      getline(dbFile, currentWord); 

      string currentLen;

      if(OrderWord(currentWord) == OrderWord(localString)){
        cout << endl << currentWord; //sent the output device and newline (start on a new line) and then display the string found in the current position of the file
        cout << "\nworking 2";
        cin >> currentLen;
      }              
    }
    dbFile.close();   
}

When I enter a valid input that has an anagram in the csv file, it just returns blank. I know that it is reading the file correctly, as I have outputted the contents of the file to console, but I think it's the searching process that is the problem...

If you require a copy of the whole code, please do let me know. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire