在C ++函数中通过引用传递向量

时间:2015-03-21 04:13:23

标签: c++ vector pass-by-reference

新生CS学生来这里。我目前正在尝试编写这个项目,这是一个使用向量的灵活待办事项列表。但是,我不能为我的生活弄清楚代码有什么问题。

应该从选项菜单功能中选择用户选择的功能,并继续要求用户将内容添加到列表中,除非他们另有选择。当用户选择“完成”选项7时,整个事情将结束,并且只有在验证了taskList数组中没有剩余项目之后才会结束。

以下是代码:

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

void options()
{
  cout << "\nWhat to do?\n\n" << endl;

  cout << "1) Add to the list. \n";
  cout << "2) Show the next item on the list. \n";
  cout << "3) Do the next item on the list, and remove it. \n";
  cout << "4) List all items \n";
  cout << "5) Save list. \n";
  cout << "6) Load list. \n";
  cout << "7) All done with this To Do List! \n\n";
}

void addToList(vector<string>& vec) //add item to list
{
  string task;
  cout << "\nPlease add an item to the to-do list: ";

  getline(cin, task);
  vec.push_back(task);
} //addToList

void showNextItem(const vector<string>& vec) //display next item in list, do not remove it
{
  cout << "\nThe next item on the list is: ";
  cout << "==> " << vec.front();
} //showNext Item

void displayAllItems(const vector<string>& vec) //display all items in list
{
  cout << "\nHere's everything you still need to do: ";
  cout << "\n\n";

  for (unsigned int i = 0; i < vec.size(); i++)
  {
    cout << vec[i];
  } //for
} //displayAllItems

void doThis(vector<string>& vec) //display item, remove from list
{
  cout << "\nOK, time to do this list item: \n";
  cout << "==> " << vec.front();

  vec.erase(vec.begin());
} //doThis

void save(const vector<string>& vec) //asks user to input file name, saves all items to file
{
  string fileName;
  cout << "\nEnter file to save items: ";

  getline(cin, fileName);

  ofstream fout(fileName.c_str());
  fout.open(fileName);

  for(unsigned int i = 0; i < vec.size(); i++)
  {
    fout << vec[i];
  } //for

  fout.close();

} //save

void load(vector<string>& vec) //asks user to input file name, loads items         from file and populates to do list with items
{
  string fileName;
  string line;
  cout << "\nEnter file to 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

提前感谢任何可以提供帮助的人!

2 个答案:

答案 0 :(得分:1)

我也是新手,我为你的计划找到了一些建议。

您无法选择其他,因为您只运行一次开关部件。所以我认为您需要while才能继续获取您的输入

  1. 我认为在选择一次号码后问题不应该停止。因此,我认为您的选择部分可能需要whilefor。并在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. 当您保存数据时,您打开文件两次,我认为这是您无法将文件写入文件的原因。当然,您需要添加一些运算符来分隔文件中的句子,以便下次可以从文件中获取。 问题也出在你的开放部分。您可以在不使用ofstream fout(fileName.c_str(), ios::out);的情况下使用此open来打开文件。如果您使用while(fin.good()),则可以从文件中多读一行。代码是:

    void save(const vecotr<string>& vec)
    {
        string fileName;
        cout <<"\nEnter file to save items: ";
        getline(cin, fileName);
        ofstream fout(fileName.c_str(), ios::out);
    
        for(unsigned int i = 0; i < vec.size(); i++)
        {
           fout <<vec[i] << "\n";
        }
        fout.close();
    } 
    
    void load(vector<string>& vec) 
    {
        string fileName;
        string line;
        cout << "\nEnter file to 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. 两件小事是你可能需要测试输入字,在显示部分你可以在行中添加num以帮助用户阅读。

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

答案 1 :(得分:0)

for(unsigned int i = 0; i < vec.size(); i++)
{
  fout << vec[i];
}

这会将字符串写入文件,但不会用空格分隔。

例如,如果向量包含{ "this", "is", "a test" },则在循环之后文件将包含thisisa test,而不是this is a testthis\nis\na test

当您尝试使用getline(fin, line)读取文件内容时,它只会向矢量添加一个字符串,内容为"thisisa test"

要解决此问题,请在fout << vec[i]之后在文件中添加换行符:

fout << vec[i] << '\n';