Как ограничить переменную v?
Мне нужно ввести и сохранить текст в файл. Для этого мне нужно ввести строку v, но при компиляции в нее вводится бесконечное количество символов. Как ее можно ограничить чтобы, например, вводилось только 5 символов?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "BoolShelf.h"
#include "Book.h"
#include <fstream>
using namespace std;
istream& operator >> (std::istream& is, vector<string> & v)
{
string temp;
// while (is >> temp) v.push_back(temp);
while (is)
{
// is >> temp;
getline(is, temp);
if (!is) break;
v.push_back(temp);
}
return is;
}
ostream& operator << (ostream& os, vector<string> & v)
{
// string val;
// while (os << val) os << v << "\n";
for (auto i : v)
{
os << i;
}
return os;
}
bool func(string& s)
{
if (s[0] == 'v')
return true;
else return false;
}
void BookShelf::SaveBook()
{
vector<string> v;
vector<string>::iterator it = v.begin();
while (it != v.end())
{
it = find_if(it, v.end(), func);
if (it != v.end())
{
cout << *it << "\n";
it++;
}
}
ofstream f1("book.txt");
f1.open("book.txt");
if (!f1.is_open())
{
cout << "Ошибка открытия файла!" << endl;
}
else
{
cout << "Файл открыт!" << endl;
cin >> v;
for (it = v.begin(); it != v.end(); it++)
f1 << *it << "\n";
}
f1.close();
}