-1

関数のような単純なジャーナルを書いていますが、ユーザーが入力した値を渡すことによってベクトルの新しい要素を生成する方法を理解できないようです。私はプログラミングに慣れていないので、答えは明らかかもしれません:/プログラムをコンパイルしてもエラーは発生しませんが、ジャーナルエントリに追加するコードは効果がないようです. 何か案は?

これが以下のプログラムです。

#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;
}
4

5 に答える 5

0
    #include <iostream>
    #include <string>
     #include <vector>

     using namespace std;
 vector<string> journal;//**SHOULD NOT WRITE INSIDE THE LOOP**
 int main()

  {
bool running = true;

while (running = true) 
{


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;

}

于 2013-04-09T11:19:56.387 に答える