0

I have written a code for word descrambling game in C++. The code is working perfectly fine. But the code is not showing any output.

Can somebody help me with what to do? I mean just tell me what am I missing to show the output of the code.

#include <bits/stdc++.h>
using namespace std;

string sortString(string word)
{
   transform(word.begin(), word.end(), word.begin(), ::toupper);

   sort(word.begin(), word.end());
   return word;
}
void jumbledString(string jumble)
{
   string checkPerWord = "";
   string userEnteredAfterSorting;

   userEnteredAfterSorting = sortString(jumble);

   ifstream words("words.txt");

   if (words) {
       while (getline(words, checkPerWord)) {

           string Ch = sortString(checkPerWord);

           if (Ch == userEnteredAfterSorting) {
               cout << checkPerWord << endl;
           }
       }
       words.close();
    }
 }

 int main()
{
   string string = "tac";
   jumbledString(string);
   return 0;
}
Software X
  • 13
  • 4
  • 2
    Without the contents of `words.txt`, it's going to be difficult to tell what's on. Essentially you are just checking if `tac` is an anagram of any of the words in the file, right? – Zoso Feb 09 '22 at 18:46
  • 2
    Also, please don't use [``](https://stackoverflow.com/q/31816095/1851678). – Zoso Feb 09 '22 at 18:51
  • `string userEnteredAfterSorting; userEnteredAfterSorting = sortString(jumble);` Any issues with copy elision? – fabian Feb 09 '22 at 18:51
  • My guess would be that opening `words` is failing – Alan Birtles Feb 09 '22 at 18:55
  • 1
    Also, a quick check, how did you verify that _code is working perfectly fine_? Did you add logs/debug etc.? – Zoso Feb 09 '22 at 18:56
  • What are the results of your debugging session? Which statement is causing the issue? What are expected values of the variables? What are the actual values of the variables? – Thomas Matthews Feb 09 '22 at 19:15
  • If you are allergic to debuggers, you can always stick "print" statement at various places to print out variable names and their values (as well as line numbers). – Thomas Matthews Feb 09 '22 at 19:17
  • this line is incorrect too: `transform(word.begin(), word.end(), word.begin(), ::toupper);`. check cpp ref for `toupper()` to see why. – Afshin Feb 09 '22 at 19:53
  • `string string = "tac";` Congratulations, this wins the title of the worst C++ line of the day. – n. m. could be an AI Feb 09 '22 at 22:53

0 Answers0