Guest User

Untitled

a guest
Mar 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main (void){
  8.  
  9. string filename;
  10. ifstream file;
  11.  
  12. // Some bool variables for the do while input loop
  13. bool isvalid = true; // Will change to false if failing the first loop
  14. bool isopened = false;
  15. bool failedopen = false;
  16.  
  17. do {
  18. if ( failedopen ){
  19. cout << "There was an error opening the file." << endl;
  20. }
  21. isvalid = true; // Reset for each loop
  22.  
  23. cout << "Enter filename: ";
  24. cin >> filename;
  25.  
  26. isvalid = validFilename(filename);
  27.  
  28. // If the filename is valid, try to open the file
  29. if(isvalid){
  30. file.open(filename);
  31. }
  32. if ( file.fail() ){
  33. // File was not opened
  34. isopened = false;
  35. }
  36. else {
  37. // File was opened;
  38. isopened = true;
  39. }
  40. }
  41. } while ( !isvalid || !isopened );
Add Comment
Please, Sign In to add comment