Advertisement
Guest User

hw 2 partial, but fixed

a guest
Oct 21st, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstring>
  6. #include <cctype>
  7. #include <ctime>
  8. using namespace std;
  9.  
  10. //structs
  11. struct Input
  12. {
  13.     int size;
  14.     string* word;
  15.     bool *is_palindrome;
  16. };
  17.  
  18. //prototypes
  19. bool openInputFile(ifstream &ifs);
  20. void File_to_Array(string*& word, int &size);
  21. void PrintArray(string*& word, int size);
  22.  
  23. //main
  24. int main()
  25. {
  26.     Input myInput = { 0, nullptr, false };
  27.     File_to_Array(myInput.word, myInput.size);//copy arr and get size
  28.     PrintArray(myInput.word, myInput.size);//print array of strings
  29.  
  30.     system("PAUSE");
  31.     return 0;
  32. }
  33.  
  34. //functions
  35. bool openInputFile(ifstream &ifs)
  36. {
  37.     string filename;
  38.  
  39.     cout << "Enter the input filename: " << endl;
  40.     getline(cin, filename);
  41.     ifs.open(filename.c_str());
  42.     return ifs.is_open();
  43. }
  44.  
  45. void File_to_Array(string*& word, int &size)//copies file to dyn arr and assigns size from first elem
  46. {
  47.     ifstream myFile;
  48.     while (!openInputFile(myFile))
  49.         cout << "Could not open file" << endl;
  50.     string tempstr = "";
  51.     getline(myFile, tempstr);//first line is size of dyn arr
  52.     size = stoi(tempstr);//now we have max size of dyn arr of strings
  53.     word = new string [size];//now we have the array of strings, *word[index] = string1
  54.     int i;
  55.  
  56.     for (i = 0; getline(myFile, word[i]) && i < size-1; ++i);//for each line
  57.     //copy line of string from file to string arr within "bool" test, second param of for loop  //copying done
  58.     size = i;
  59.     myFile.close();//done with file, no need, close it
  60. }
  61.  
  62. void PrintArray(string*& word, int size)
  63. {
  64.     for (int i = 0; i < size; i++)
  65.         cout << word[i] << endl;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement