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

Write a C++ program that reads in 10 English and 10 French words and stores each

ID: 3575533 • Letter: W

Question

Write a C++ program that reads in 10 English and 10 French words and stores each English word and its French equivalent in a Dictionary object. The dictionary data is stored in a file, call it dict.dat. Sort the array of 10 Dictionary objects by the English word, and then allow the user to enter an English word and see its French equivalent. Remember to overload the assignment operator for the Dictionary class and use dynamic memory allocation to store the print its French equivalent. You should have a header file called dictionary.h, and a method file called dictionary.cpp, the main should be called dictionarydrvr.cpp

Explanation / Answer

//Dictionary.h

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Dictionary
{
   string English;
   string French;
public:
   //default constructor initializes private memebers
   Dictionary();
   void set(string eng, string frn);
   Dictionary operator=(const Dictionary &obj);
   friend void sort(Dictionary obj[],int size);
   friend ostream &operator<<(ostream &out, Dictionary obj);
   friend Dictionary search(Dictionary obj[], string search, int size);
};

-------------------------------------------------------------------------------------

//Dictionary.cpp

#include"Dictionary.h"

Dictionary::Dictionary()
{
   English = "";
   French ="";
}
//overloaded constructor
void Dictionary::set(string eng, string frn)
{
   English = eng;
   French = frn;
}

Dictionary Dictionary::operator = (const Dictionary &obj)
{
   English = obj.English;
   French = obj.French;
   return *this;
}

void sort(Dictionary obj[], int size)
{
   string tmp;

   for (int i = 0; i < size; i++)
   {
       for (int j = i + 1; j < size; j++)
       {
           if (obj[i].English > obj[j].English)
           {
               tmp = obj[i].English;
               obj[i].English = obj[j].English;
               obj[j].English = tmp;
           }
       }
   }
}

ostream &operator<<(ostream &out, Dictionary obj)
{
   out << obj.English << " " << obj.French << endl;
   return out;
}
Dictionary search(Dictionary obj[], string search, int size)
{
   Dictionary tmp;
   int found = 0;

   for (int i = 0; i < size; i++)
   {
       if (obj[i].English == search)
       {
           tmp = obj[i];
           found = 1;
           break;
          
       }
   }
   if ( !found )
       cout << "English word not found" << endl;
   return tmp;
}

--------------------------------------------------------------------------------------------------------------

//main.cpp

#include"Dictionary.h"
//MAximum 200 words to hold in array of Dictionary
#define MAX 200


int main()
{
   //declare array of Dictionary objects to hold data read from file Dict.dat
   Dictionary dict[MAX];
   //declare count variable to hold the number of line read from file
   int count = 0;
   string eng, frn;
   //input stream object to open file Dict.dat
   ifstream in;
   in.open("Dict.dat");

   //check if file can be opened else return error
   if (!in)
   {
       cout << "Input file can't be open" << endl;
       return -1;
   }
   //read file till end of file
   while (!in.eof())
   {
       in >> eng;
       getline(in,frn);
       //set words for Dictionary object
       dict[count++].set(eng, frn);
   }

   //print dictionary before sort
   cout << "Before soring...." << endl;
   for (int i = 0; i < count; i++)
   {
       cout << dict[i];
   }
   //sort the dictionaryby calling sort of
   sort(dict, count);
   //print dictionary after sort
   cout << "After soring...." << endl;
   for (int i = 0; i < count; i++)
   {
       cout << dict[i];
   }
   //allowe user to search dictionary by english word
   cout << "-------------------------------------------------------------------"<<endl;
   cout << "Enter English word to search: ";
   cin >> eng;

   //call search function
   Dictionary found = search(dict, eng, count);
   cout << found;

}

-----------------------------------------------------------------

output

Before soring....
house maison
mother mere
father pere
sister socur
brother frere
library bibliotheque
door porte
hello bonjour
good-bye au revoir
cat chat
dog chien
book livre
After soring....
book maison
brother mere
cat pere
dog socur
door frere
father bibliotheque
good-bye porte
hello bonjour
house au revoir
library chat
mother chien
sister livre
-------------------------------------------------------------------
Enter English word to search: library
library chat

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote