In C++ Determine if a word entered by the user is spelled correctly. A word is c
ID: 3574881 • 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).
Example Output:
intrest ENTER anchor ENTER fake ENTER hax ENTE ENTER what ENTER 36 ENTE exit Enter word to spellcheck (or exit to end Mn intrest is not spelled correctly. An Enter word to spellcheck (or exit to end)An anchor is spelled correctly. An Enter word to spellcheck (or exit to end Mn fake is spelled correctly. An Enter word to spellcheck (or exit to end) In hax is not spelled correctly. In Enter word to spellcheck (or exit to end Mn wut is not spelled correctly. Nn Enter word to spellcheck (or exit to end) In what is spelled correctly. An Enter word to spellcheck (or exit to end Mn 36 is not spelled correctly. An Enter word to spellcheck (or exit to end) In Ending program. In dict txtExplanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
// number of words in file
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 spell check( or exit to end):";
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 spell check( or exit to end):";
cin >> text;
}
cout << "Ending program" << endl;
}
/*
dict.txt
interest
anchor
fake
what
sample output
Enter word to spell check( or exit to end): intret
intret is not spelled correctly.
Enter word to spell check( or exit to end): fake
fake is spelled correctly.
Enter word to spell check( or exit to end): interest
interest is spelled correctly.
Enter word to spell check( or exit to end): ham
ham is not spelled correctly.
Enter word to spell check( or exit to end): what
what is spelled correctly.
Enter word to spell check( or exit to end): exit
Ending program
*/