For C++, how do I read through a file called "dictionary.txt" and see if the word user-inputted is in the file, but using cstring only and NOT string.
For example,
if the dictionary.txt is like
Another
ostentatious
work
extra
dog
camera
hinder
the output should look like this,
Enter a word to be looked up in dictionary: cats
cats is NOT in the dictionary.
Enter a word to be looked up in dictionary: ostentatious
ostentatious IS in the dictionary.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
ifstream infile;
char str[20],temp[20];
int i,opt;
infile.open("dictionary.txt"); //open the file
dictionary.txt
if(!infile)
{
cout<<endl<<"Unable to open the file";
return 0;
}
cout<<endl<<"Enter a word to be
looked u in dictionary : ";
cin>>temp; //read the word to lookup in
dictionary
//loop to read the file till end
while(!infile.eof())
{
infile>>str;//read a word
from file
//compare the word with the word in
file
for(i=0;temp[i]!='\0';i++)
if(temp[i]!=str[i])
break;
if(temp[i]=='\0' &&
str[i]=='\0')
{
cout<<endl<<temp<<" is in the dictionary.";
opt=1;
break;
}
}
if(opt!=1)
{
cout<<endl<<temp<<" is not in the
dictionary.";
}
infile.close();//close the file
}
output
Get Answers For Free
Most questions answered within 1 hours.