Passing vectors by reference in C++ functions -


freshman cs student here. i'm trying write project, flexible to-do list using vectors. however, can't life of me figure out what's wrong code.

it's supposed select function chosen user options menu function, , continue ask user add things list unless choose otherwise. whole thing end when user chooses "done" option 7, , after that's verified there no items left in tasklist array.

here's code:

#include <fstream> #include <iomanip> #include <iostream> #include <string> #include <sstream> #include <vector>  using namespace std;  void options() {   cout << "\nwhat do?\n\n" << endl;    cout << "1) add list. \n";   cout << "2) show next item on list. \n";   cout << "3) next item on list, , remove it. \n";   cout << "4) list items \n";   cout << "5) save list. \n";   cout << "6) load list. \n";   cout << "7) done list! \n\n"; }  void addtolist(vector<string>& vec) //add item list {   string task;   cout << "\nplease add item to-do list: ";    getline(cin, task);   vec.push_back(task); } //addtolist  void shownextitem(const vector<string>& vec) //display next item in list, not remove {   cout << "\nthe next item on list is: ";   cout << "==> " << vec.front(); } //shownext item  void displayallitems(const vector<string>& vec) //display items in list {   cout << "\nhere's still need do: ";   cout << "\n\n";    (unsigned int = 0; < vec.size(); i++)   {     cout << vec[i];   } //for } //displayallitems  void dothis(vector<string>& vec) //display item, remove list {   cout << "\nok, time list item: \n";   cout << "==> " << vec.front();    vec.erase(vec.begin()); } //dothis  void save(const vector<string>& vec) //asks user input file name, saves items file {   string filename;   cout << "\nenter file save items: ";    getline(cin, filename);    ofstream fout(filename.c_str());   fout.open(filename);    for(unsigned int = 0; < vec.size(); i++)   {     fout << vec[i];   } //for    fout.close();  } //save  void load(vector<string>& vec) //asks user input file name, loads items         file , populates list items {   string filename;   string line;   cout << "\nenter file load to-do list: ";    getline(cin, filename);   ifstream fin(filename.c_str());    fin.open(filename);    while (fin.good())   {     getline(fin, line);     vec.push_back(line);   } //while    fin.close();  } //load  bool alldone() //displays goodbye message, exits {     bool done = true;     cout << "\nall done!";     return done; } //all done  int main() {   int option;   bool done = false;   string useroption;   stringstream mystr;   vector<string> tasklist;      options();     cout << "==> ";     getline(cin, useroption);     mystr << useroption;     mystr >> option;      switch(option)     {       case 1:         addtolist(tasklist);         break;       case 2:         shownextitem(tasklist);         break;       case 3:         dothis(tasklist);         break;       case 4:         displayallitems(tasklist);         break;       case 5:         save(tasklist);         break;       case 6:         load(tasklist);         break;       case 7:         alldone();         break;     } //switch } //main 

thanks in advance can help!

i'm new this, , find suggestions program.

you can't choose other because run switch part once. think need while continue input

  1. i think problem should not stop after choose number once. think may need while or for choose part. , add return @ case 7.

    while(1) {     options();     cout << "==> ";     getline(cin, useroption);     option = atoi(useroption.c_str());     switch(option)     {     case 1:        addtolist(tasklist);        break;     case 2:        shownextitem(tasklist);        break;     case 3:        dothis(tasklist);        break;     case 4:        displayallitems(tasklist);        break;     case 5:        save(tasklist);        break;     case 6:        load(tasklist);        break;     case 7:        cout << "\nall done!\n";        return 0;     default:        return -1; } cout<<"\n========================"<<endl; 

    }

  2. when save data, open file twice, think that's reason can't write your file. of course, need add operator separate sentences in file can file next time. problem in open part. can open file use ofstream fout(filename.c_str(), ios::out); without open. , if use while(fin.good()), may read 1 line more file. code is:

    void save(const vecotr<string>& vec) {     string filename;     cout <<"\nenter file save items: ";     getline(cin, filename);     ofstream fout(filename.c_str(), ios::out);      for(unsigned int = 0; < vec.size(); i++)     {        fout <<vec[i] << "\n";     }     fout.close(); }   void load(vector<string>& vec)  {     string filename;     string line;     cout << "\nenter file load to-do list: ";     getline(cin, filename);    ifstream fin(filename.c_str());     while(getline(fin, line))    {        vec.push_back(line);    } //while    cout <<"======" <<vec.size()<<endl;     fin.close(); }  
  3. two little thing may need test input word , in display part can add num in line user read.

    void displayallitems(const vector<string>& vec) {      cout << "\nhere's still need do: ";      cout << "\n\n";       (unsigned int = 0; < vec.size(); i++)      {          cout << <<"."<< vec[i] << endl;      } } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -