【发布时间】:2015-05-24 14:36:10
【问题描述】:
这里是 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
提前感谢任何可以提供帮助的人!
【问题讨论】:
-
什么地方不能正常工作,你希望它如何工作?
-
你有什么问题?
-
@AtlasC1:我希望用户输入的选项(int 选项)能够选择其对应的功能。 1为addToList,2为showNextItem等。问题是当我运行代码时,无论选择哪个选项,都只执行第一个程序addToList。我还试图找出一种在 bool allDone 返回 true 时终止程序的方法,但前提是向量中没有更多项目。抱歉,如果到目前为止还不清楚,仍然刚刚开始学习。
标签: c++ vector pass-by-reference