By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

Why won't this code open the .txt files?

P: 1
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7.  
  8. {
  9.     cout << "Willkommen zu meinem Programm!\n\n";
  10.     char choice;
  11.     do {
  12.         //User Prompt
  13.         cout << "Please Choose a Selection from the Menu.\n\n";
  14.         //Display Menu
  15.         cout << "1) Program Languages and their uses\n2) Different Programming Paradigms\n3) Different C++ Features and their uses \n4) Exit\n\n";
  16.         cin >> choice;
  17.         cout << endl;
  18.         if (choice == '1')
  19.         {
  20.             //Read the file
  21.             ifstream infile;
  22.             ofstream outfile;
  23.             //Open the File
  24.             infile.open("ProgrammingLanguages.txt", ios::in);
  25.             //Ensure File exists
  26.             if (!infile)
  27.             {
  28.                 cout << "File does not exist!\n\n";
  29.             }
  30.             //Close the file
  31.             infile.close();
  32.         }
  33.         if (choice == '2')
  34.         {
  35.             //Read the file
  36.             ifstream infile;
  37.             ofstream outfile;
  38.             //Open the File
  39.             infile.open("ProgrammingParadigms.txt", ios::in);
  40.             //Ensure File exists
  41.             if (!infile)
  42.             {
  43.                 cout << "File does not exist!\n\n";
  44.             }
  45.             //Close the file
  46.             infile.close();
  47.         }
  48.         if (choice == '3')
  49.         {
  50.             //Read the file
  51.             ifstream infile;
  52.             ofstream outfile;
  53.             //Open the File
  54.             infile.open("C++Features.txt", ios::in);
  55.             //Ensure File exists
  56.             if (!infile)
  57.             {
  58.                 cout << "File does not exist!\n\n";
  59.             }
  60.             //Close the file
  61.             infile.close();
  62.         }
  63.         else if (choice == '4')
  64.         {
  65.             //Tell user to have nice day
  66.             cout << "A thousand blessings upon you and your family.\n";
  67.             cin.get();
  68.             cin.ignore();
  69.     }
  70.  
  71.     } while (choice != '4');
  72.  
  73.     return 0;
  74.  
  75. }
Apr 12 '16 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
In each of your menu choices you define an infile variable. When the closing brace occurs, the variable is now out of scope and can't be used any further in the program.

Just define one infile variable for the entire main() function.
Apr 12 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.