candyapplecorn

getFileName(ifstream &file)

Jan 19th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. //============================================================================================================
  2. // getFileName takes a file stream object by reference, and after asking the user for input via a numeric
  3. // choice, executes a switch block, opening 1 of 3 files.
  4. //
  5. // I wanted to have the user type a file name, literally. For example, the user would type "numbers1.txt". I
  6. // wanted that to go into a string, and then call file.open(string);. However as far as I know and from my
  7. // little research on the internet, it turns out ".open()" only takes a string literal as an argument. So I had
  8. // to take the easy way out and just make a switch statement whose .open() function calls already have string
  9. // literals.
  10. // I am not happy with this answer. I want the user to be able to drop any text file into the right folder
  11. // and have the program use it. For example, "clownsandballoons.txt". But until I find out how to do that, this
  12. // is as good as I can do.
  13. //============================================================================================================
  14. void getFileName(ifstream &file)
  15. {
  16.     int choice;
  17.     cout << "Enter 1 to open numbers1.txt, 2 to open numbers2.txt, and 3 to open numbers3.txt\n\t";
  18.     while(cin >> choice, (choice > 3 || choice < 1))
  19.     {
  20.         cout << "Enter 1, 2 or 3.\n\t";
  21.         cin >> choice;
  22.     }
  23.     switch(choice)
  24.     {
  25.     case 1:
  26.         file.open("numbers1.txt");
  27.         break;
  28.     case 2:
  29.         file.open("numbers2.txt");
  30.         break;
  31.     case 3:
  32.         file.open("numbers3.txt");
  33.         break;
  34.     }
  35.  
  36.     return;
  37. }
Add Comment
Please, Sign In to add comment