Write code that allows a user to make a grocery list. The program will also allo
ID: 3545516 • Letter: W
Question
Write code that allows a user to make a grocery list. The program will also allow the user to export the grocery lists to a
comma delimited file that can be read by a spreadsheet program (i.e., CSV file) No classes are needed, just a main function.
The function declarations are:
void DisplayMenu();
void CreateList( map< string, vector<string> >& lists );
void ExportLists( map< string, vector<string> >& lists );
The main program must:
1. Use the DisplayMenu function to display the menu.
2. Prompt the user for a menu choice.
3. Use the CreateList and ExportLists functions to execute the user's choice.
4. Exit if the user chooses to exit.
5. Re-prompt the user for invalid choices.
Explanation / Answer
#include <iostream>
#include <map>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;
void DisplayMenu();
void CreateList( map< string, vector<string> >& lists );
void ExportLists( map< string, vector<string> >& lists );
int main(){
map< string, vector<string> > lists;
while(true){
DisplayMenu();
int i;
cin>>i;
switch(i){
case 1 : CreateList(lists);break;
case 2 : ExportLists(lists);break;
case 3 : exit(0);
}
}
}
void DisplayMenu(){
cout << "1. Create a new grocery list 2. Export grocery lists 3. Quit"<<endl;
}
void CreateList(map< string, vector<string> >& lists ){
string name;
cout << "enter store name : ";
cin>>name;
lists[name];
cout << "enter the list of items in the store ";
string temp;
cin>>temp;
while(temp != "done"){
lists[name].push_back(temp);
cout << "Added item in " << name <<" is : "<<temp<<endl;
cin>>temp;
}
}
void ExportLists( map< string, vector<string> >& lists ){
char output[100];
cout << "Enter the output file name : ";
cin >> output;
ofstream myfile(output,ios::trunc);
myfile << "Grocery Store," << "Item" <<endl;
for (map<string, vector<string> >::iterator it=lists.begin(); it!=lists.end(); ++it){
for (vector<string>::iterator it1 = it->second.begin() ; it1 != it->second.end(); ++it1)
myfile << it->first <<","<<*it1<<endl;
}
myfile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.