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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.