c++ - Map iterator issues. Code included -
so here s code, dont understand why i'm getting these errors. issue within export recipes function.
#include<iostream> #include<fstream> #include<map> #include<vector> #include<string> using namespace std; void displaymenu(); void addrecipe( map< string, vector<string> >& recipes ); void exportrecipes( map< string, vector<string> >& recipes ); int main ( void ){ int choice = 0; bool done = false; map< string, vector<string> > recipes; while ( done == false ){ displaymenu(); cin >> choice; if ( choice == 3 ){ done = true; } else if ( choice == 2 ){ exportrecipes( recipes ); } else if ( choice == 1 ){ addrecipe( recipes ); } } } void displaymenu(){ cout << "1. add recipe " << endl; cout << "2. export recipes " << endl; cout << "3. exit" << endl; } void addrecipe( map< string, vector<string> >& recipes ){ string name, ingredient; bool done = false; cout << "enter recipe name: "; cin >> name; while ( done == false ){ cout << "enter new ingredient , amount( enter done exit )" << endl; getline( cin , ingredient, '\n' ); if ( ingredient == "done" ){ done = true; } if( ingredient != "done"){ recipes[ name ].push_back( ingredient ); cout << "added \"" << ingredient << "\"." << endl << endl; } } } void exportrecipes( map< string, vector<string> >&recipes ){ ofstream outfile; outfile.open( "recipes.txt" ); ( map< string, vector<string> >::iterator recipe = recipes.begin(); recipe != recipes.end(); recipe++ ) { outfile << endl << endl << recipe -> first << endl; ( map< string, vector<string> >::iterator ingredients = recipe->second.begin(); ingredients != recipe->second.end(); ingredients++ ) { outfile << "\t" << *ingredients << endl; } } }
if iterate through first loop in export can key, cant value @ all.
why have second loop map. recipe->second vector try -
void exportrecipes( map< string, vector<string> >&recipes ){ ofstream outfile; outfile.open( "recipes.txt" ); ( map< string, vector<string> >::iterator recipe = recipes.begin(); recipe != recipes.end(); recipe++ ) { outfile << endl << endl << recipe -> first << endl; ( vector<string>::iterator ingredients = recipe->second.begin(); ingredients != recipe->second.end(); ingredients++ ) { outfile << "\t" << *ingredients << endl; } } }
Comments
Post a Comment