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

Need help debugging the following code: #include <iostream> #include <set> #incl

ID: 3560608 • Letter: N

Question

Need help debugging the following code:

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

using namespace std;
using std::cin;
int main()
{
   multiset<string> M1;
   multiset<string>::iterator iter;

   char coice;
   string str;
   ofstream fout;
   ifstream fin;

   fin.open("temp.txt");
   getline(fin, str);
   while (!fin.eof())
   {
       M1.insert(str);
       getline(fin, str);
   }

   do
   {
       cout << "Menu ";
       cout << "Enter choice: ";
       cin >> choice;
       cin.get();
       switch(choice)
       {
       case 'a':
           cout << "Enter the new item to be added ";
           getline(cin, str);
           M1.insert(str);
           break;
       case 'b':
           cout << "There are "<<M1.count()<<" elements ";
           break;
       case 'c':
           for (iter = M1.begin();iter !=M1.end();++iter)
           {
               cout<< *iter <<endl;
           }
           break;
       case 'd':
           cout<<"Enter the string to erase: ";
           getline(cin, str);
           M1.erase(str);
           break;
       default:
           cout<<"Invalid";
       }
   }while (choice !='q');
   fout.open("temp.txt");
   for (iter = M1.begin();iter !=M1.end();++iter)
   {
       cout<< *iter<<endl;
   }
   cin.get();
}

I get the following errors upon compiling:

Explanation / Answer

There seems to be only minor errors. Your variable declaration is mispelled. It should be char choice and not char coice. And your use of M1.count() is incorrect. count expects a value that is to be searched. What you might be looking for is M1.size() instead.

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