Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Determine if a word entered by the user is spelled correctly. A word is c

ID: 3575338 • Letter: I

Question

In C++ Determine if a word entered by the user is spelled correctly. A word is considered correct if it's found in dict.txt (see required output to get file).

Im having trouble with my code reading the words spelled correctly.

My code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{

int n = 4;
string myArray[n];
ifstream file("dict.txt");
if(file.is_open())
{
for(int i = 0; i < n; ++i)
{
file >> myArray[i];
}
}
string text=" ";

cout << "Enter word to spellcheck (or exit to end)" << endl;
cin >> text;

while(text.compare("exit")!=0)
{

int flag = true;
for(int i=0;i<n;++i)
{
if(text.compare(myArray[i]) == 0)
{
flag = false;
break;
}
}
if (flag)
cout << text << " is not spelled correctly."<< endl;
else
cout << text << " is spelled correctly." << endl;
cout << "Enter word to spellcheck (or exit to end)" << endl;
cin >> text;
}
cout << "Ending program..." << endl;
}

Output:

Standard input intrest anchor fake hax wut what 36 exit Required output Enter word to spellcheck (or exit to end) Vn intrest is not spelled correctly. Enter word to spellcheck (or exit to end anchor is spelled correctly. Enter word to spellcheck (or exit to end)An fake is spelled correctly.An Enter word to spellcheck (or exit to end)An hax is not spelled correctly.An Enter word to spellcheck (or exit to end)An Wut is not spelled correctly. n Enter word to spell check (or exit to end n n What is spelled correctly. Enter word to spell check (or exit to end)Nn 36 is not spelled correctly. Anl Enter word to spellcheck (or exit to end)An Ending program ..An Files in the same directory dict txt Your Program's output Enter word to spellcheck (or exit to end)An intrest is not spelled correctly. An Enter word to spellcheck (or exit to end) Mn anchor is not spelled correctly. An Enter word to spellcheck (or exit to end)An fake is not spelled correctly. An Enter word to spell check (or exit to end)And hax is not spelled correctly. And Enter word to spellcheck (or exit to end) An Wut is not spelled correctly. An Enter word to spellcheck (or exit to end) n what is not spelled correctly. Mn Enter word to spellcheck (or exit to end)An 36 is not spelled correctly An Enter word to spell check (or exit to end n Ending program ..An

Explanation / Answer

// C++ code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
   int n = 4;
   string myArray[n];
   ifstream file("dict.txt");

   if(file.is_open())
   {
       // only 4 strings will be read in myArray
       for(int i = 0; i < n; ++i)
       {
           file >> myArray[i];
       }
   }
   // file should be closed after reading
   file.close();

   string text;
   cout << "Enter word to spellcheck (or exit to end): " << endl;
   cin >> text;

   while(text.compare("exit")!=0)
   {
       int flag = true;
       for(int i=0;i<n;++i)
       {
           if(text.compare(myArray[i]) == 0)
           {
               flag = false;
               break;
           }
       }
      
       if (flag)
           cout << text << " is not spelled correctly."<< endl;
       else
           cout << text << " is spelled correctly." << endl;
      
       cout << "Enter word to spellcheck (or exit to end): " << endl;
       cin >> text;
   }
   cout << "Ending program..." << endl;
}


/*

dict.txt
anchor
fake
what
interest

output:

Enter word to spellcheck (or exit to end):
intrest
intrest is not spelled correctly.
Enter word to spellcheck (or exit to end)
anchor
anchor is spelled correctly.
Enter word to spellcheck (or exit to end)
fake
fake is spelled correctly.
Enter word to spellcheck (or exit to end)
hax
hax is not spelled correctly.
Enter word to spellcheck (or exit to end)
wut
wut is not spelled correctly.
Enter word to spellcheck (or exit to end)
what
what is spelled correctly.
Enter word to spellcheck (or exit to end)
36
36 is not spelled correctly.
Enter word to spellcheck (or exit to end)
exit
Ending program...


*/