我想制作一个程序,它将从用户处获取字符串并将它们存储在一个向量中。一旦将它们存储在矢量中,它们可以被显示,移除或打印到文件中。我正在创建显示向量中元素的程序部分,但我似乎无法使其工作。 这里是我的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中的任何东西?
另外:有没有人有一些建议,指出我在正确的方向如何从我的载体中删除元素,以及如何将我的载体元素打印到文件?
请不要更加精确地使用'goto'语句 – Ceros
http://en.cppreference.com/w/cpp/container/vector http://en.cppreference.com/w/cpp/container/vector/erase – Ceros
您的程序设计有许多缺陷。 – Naidu