我正在编写一个类似函数的简单日志,但是我似乎无法理解如何通过传递用户输入的值来生成向量的新元素。我是编程新手,所以答案可能很明显:/编译程序时没有错误,但添加日记条目的代码似乎没有效果。有任何想法吗?
这是下面的程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
bool running = true;
while (running = true)
{
vector<string> journal;
vector<string>::const_iterator iter;
int count = 1;
journal.push_back("Day 1. Found some beans.");
journal.push_back("Must remember not to eat beans");
journal.push_back("Found some idiot who traded beans for a cow!");
cout << "Journal Tester.\n\n";
cout << "1 - View Journal\n2 - Add journal entry\n";
cout << "3 - Quit\n";
cout << "\nPlease choose: ";
string newentry;
int choice;
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
for (iter = journal.begin(); iter != journal.end(); ++iter)
{
cout << "Entry " << count << ": \n";
cout << *iter << endl;
++ count;
}
count = 1;
break;
case 2:
cout << "\nYou write: ";
cin >> newentry;
cout << endl << newentry;
journal.push_back(newentry);
break;
}
}
return 0;
}