1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
void SetArray(string str_array[], int array_size);
void PrintArray(string str_array[], int array_size);
void SaveArray(string str_array[], int array_size);
void SetVector(vector<string>& str_vect, int vector_size);
void PrintVector(vector<string>& str_vect, int vector_size);
void SaveVector(vector<string>& str_vect, int vector_size);
inline void Ignore();
int main()
{
/*
cout << "How many elements will this array have?" << endl;
cout << "#";
int array_size;
cin >> array_size;
Ignore();
string str_array[array_size];
SetArray(str_array, array_size);
PrintArray(str_array, array_size);
SaveArray(str_array, array_size);
*/
cout << "How many elements will the vector have?" << endl;
cout << "#";
int vector_size;
cin >> vector_size;
Ignore();
vector<string> str_vect(vector_size);
SetVector(str_vect, vector_size);
PrintVector(str_vect, vector_size);
SaveVector(str_vect, vector_size);
return 0;
}
void SetArray(string str_array[], int array_size)
{
int elementNumber = 0;
for(int i = 0; i < array_size; i++)
{
cout << "Enter string for element #" << ++elementNumber << ": ";
getline(cin, str_array[i]);
}
}
void PrintArray(string str_array[], int array_size)
{
cout << endl;
for(int i = 0; i < array_size; i++)
{
cout << str_array[i] << endl;
}
}
void SaveArray(string str_array[], int array_size)
{
ofstream save;
save.open("SaveFile.txt");
for(int i = 0; i < array_size; i++)
{
save << str_array[i] << endl;
}
}
void SetVector(vector<string>& str_vect, int vector_size)
{
int elementNumber = 0;
string elementString;
for(int i = 0; i < str_vect.size(); i++)
{
if(elementNumber == vector_size)
{
break;
}
cout << "Enter information into vector for element #" << ++elementNumber << ": ";
getline(cin, elementString);
str_vect.push_back(elementString);
}
}
void PrintVector(vector<string>& str_vect, int vector_size)
{
for(int j = 0; j < str_vect.size(); j++)
{
cout << str_vect[j] << endl;
}
}
void SaveVector(vector<string>& str_vect, int vector_size)
{
ofstream saveVector;
saveVector.open("Vector Info.txt");
cout << "\nVector info saved..." << endl;
for(int sv = 0; sv < str_vect.size(); sv++)
{
saveVector << str_vect[sv] << endl;
}
}
inline void Ignore()
{
cin.ignore();
}
|