Guest User

Untitled

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