2016-09-27 47 views
0

我想制作一个程序,它将从用户处获取字符串并将它们存储在一个向量中。一旦将它们存储在矢量中,它们可以被显示,移除或打印到文件中。我正在创建显示向量中元素的程序部分,但我似乎无法使其工作。 这里是我的WIP:如何输出矢量?

#include <iostream> 
#include <cctype> 
#include <ctype.h> 
#include <algorithm> 
#include <string> 
#include <math.h> 
#include <windows.h> 
#include <iomanip> 
#include <vector> 
#include <ostream> 
#include <sstream> 
#include <fstream> 
#include <istream> 

// common namespace 
using namespace std; 

int main() 
{ 
    start:int option; 
    cout << "Welcome to MyBook! - A text recorder framework application." << endl; 
    cout << "-----------------------------------------------------------" << endl; 
    cout << "Main Menu:" << endl; 
    cout << "1 - Add a book to the collection." << endl; 
    cout << "2 - Display all books currently in the collection." << endl; 
    cout << "3 - Remove a book from the collection." << endl; 
    cout << "4 - Write stored date to file." << endl; 
    cout << "5 - Quit" << endl; 
    cout << "Make your selection: "; 
    cin >> option; 

    vector<string> bookCollection; 
    string entryVal = " "; 
    string anotherOption; 

    **if (option == 1) 
    { 
     // add a book to the collection 
     cin.ignore(); 
    enterbook:cout << "Add a book to the collection: "; 
     getline(cin, entryVal); 
     bookCollection.push_back(entryVal); 

     cout << "Would you like to enter another book?(1 - yes, 0 - no): "; 
     getline(cin, anotherOption); 
     //cout << "test"; 

     if (anotherOption == "1") 
     { 
      goto enterbook; 
     } 

     else if (anotherOption == "0") 
     { 
      goto start; 
     } 

    } 

    else if (option == 2) 
    { 
     for (int i = 0; i < bookCollection.size(); i++) 
      cout << bookCollection[i] << " "; 
    }** 

    else if (option == 3) 
    { 
     // remove a book from the collection 
    } 

    else if (option == 4) 
    { 
     //write to file 
    } 

    else if (option == 5) 
    { 
     //Nested IF to kill program properly 
     int quitVar; 
     cout << "Are you sure you want to exit the program?: "; 
     cin >> quitVar; 

     if (quitVar == 1) 
     { 
      cout << "The program will now be terminated." << endl; 
      return 0; 
     } 

     else if (quitVar == 0) 
     { 
      cout << "Returning to the main menu." << endl; 
      goto start; 
     } 
    } 
} 

我的问题是:为什么我不能打印出bookCollection?或者更好的是,甚至存储在bookCollection中的任何东西?

另外:有没有人有一些建议,指出我在正确的方向如何从我的载体中删除元素,以及如何将我的载体元素打印到文件?

+5

请不要更加精确地使用'goto'语句 – Ceros

+0

http://en.cppreference.com/w/cpp/container/vector http://en.cppreference.com/w/cpp/container/vector/erase – Ceros

+0

您的程序设计有许多缺陷。 – Naidu

回答

1

鉴于vector<string> bookCollection,你可以转储其内容到一个文件,说 “foo.txt的”,使用ostream_iterator

copy(cbegin(bookCollection), cend(bookCollection), ostream_iterator<string>(ofstream("foo.txt"), "\n")) 

你可以删除元素,如string entryVal,使用remove

bookCollection.resize(bookCollection.size() - distance(remove(begin(bookCollection), end(bookCollection), entryVal), end(bookCollection)); 
1

我的问题是:为什么我不打印bookCollection?或者更好的是,甚至存储在bookCollection中的任何东西?

添加元素后,您正在跳转到start(后用户按下0)位于在main()的最顶端,它可能会导致你的载体被清除,并重新初始化。根据标准,你的代码是正确的,但是你从范围集合范围跳到它不是C-tor和d-tor的地方:6.7/3(例子更加相关):

可以将其转换为块,但不能以绕过具有初始化的声明的方式。 A 程序从具有自动存储持续时间的变量不在范围内的点跳转到范围内的 点是格式错误的,除非变量具有标量类型,类型具有简单的默认构造函数 构造函数和平凡的析构函数,这些类型之一的cv限定版本,或者前面类型之一的数组,并且声明时没有初始值设定项(8.5)。

[ Example: 
void f() { 
// ... 
goto lx; // ill-formed: jump into scope of a 
// ... 
ly: 
X a = 1; 
// ... 
lx: 
goto ly; // OK, jump implies destructor 
// call for a followed by construction 
// again immediately following label ly 
} 
—end example ] 

也许你会看到一些你的收藏中,一旦你下面bookCollection声明移动start:。但是 - 我强烈建议用plain old while循环代替goto