Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstring>
- #include <cctype>
- #include <ctime>
- using namespace std;
- //structs
- struct Input
- {
- int size;
- string* word;
- bool *is_palindrome;
- };
- //prototypes
- bool openInputFile(ifstream &ifs);
- void File_to_Array(string*& word, int &size);
- void PrintArray(string*& word, int size);
- //main
- int main()
- {
- Input myInput = { 0, nullptr, false };
- File_to_Array(myInput.word, myInput.size);//copy arr and get size
- PrintArray(myInput.word, myInput.size);//print array of strings
- system("PAUSE");
- return 0;
- }
- //functions
- bool openInputFile(ifstream &ifs)
- {
- string filename;
- cout << "Enter the input filename: " << endl;
- getline(cin, filename);
- ifs.open(filename.c_str());
- return ifs.is_open();
- }
- void File_to_Array(string*& word, int &size)//copies file to dyn arr and assigns size from first elem
- {
- ifstream myFile;
- while (!openInputFile(myFile))
- cout << "Could not open file" << endl;
- string tempstr = "";
- getline(myFile, tempstr);//first line is size of dyn arr
- size = stoi(tempstr);//now we have max size of dyn arr of strings
- word = new string [size];//now we have the array of strings, *word[index] = string1
- int i;
- for (i = 0; getline(myFile, word[i]) && i < size-1; ++i);//for each line
- //copy line of string from file to string arr within "bool" test, second param of for loop //copying done
- size = i;
- myFile.close();//done with file, no need, close it
- }
- void PrintArray(string*& word, int size)
- {
- for (int i = 0; i < size; i++)
- cout << word[i] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement