Reputation: 11
I am trying to make a program that will get strings from a user and store them in a vector. Once they are stored in a vector they can be displayed, removed or printed to a file. I am in the process of creating the portion of the program that displays the elements in the vector but I cant seem to get it to work. Here is my 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;
}
}
}
My question is: Why am I not printing out bookCollection? Or better yet, is anything even being stored in bookCollection?
In addition: Does anyone have some suggestions to point me in the right direction on how to remove elements from my vector and also how to print the elements of my vector to a file?
Upvotes: 0
Views: 339
Reputation: 38919
Given vector<string> bookCollection
, you can dump its contents to a file, say "foo.txt", using an ostream_iterator
:
copy(cbegin(bookCollection), cend(bookCollection), ostream_iterator<string>(ofstream("foo.txt"), "\n"))
You could remove an element, such as string entryVal
, using remove
:
bookCollection.resize(bookCollection.size() - distance(remove(begin(bookCollection), end(bookCollection), entryVal), end(bookCollection));
Upvotes: 1
Reputation: 49986
My question is: Why am I not printing out bookCollection? Or better yet, is anything even being stored in bookCollection?
After adding elements, You are making a jump to start
(after user pressed 0) which is located at the very top of main()
, it probably causes your vector to be cleared and reinitialized. According to standard your code is correct, but the fact that you jump from the scope where collection is in scope to place where it is not a c-tor and d-tor is being called on it: 6.7/3 (example is more revelant):
It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (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 ]
Probably you would see something in your collection once you moved start:
below bookCollection
declaration. But - I would strongly suggest to replace goto
with plain old good while
loop.
Upvotes: 1