Resetting a Do While loop

First time on this site and a noob to cpp, so forgive me if my question seems stupid to some.
Im writing a File I/O program that takes the input file, reads it, filters it contents, and puts those filtered contents into a output file. I have it working nicely except for one problem: When I run the loop again, it does not recognize the input file. Im thinking it still has the file open from the previous loop and therefore cant open two at the same time. How do I reset my do while loop?

Here's my source code (its rough copy):



#include <iostream>
#include <fstream>
#include <cctype> // needed for to upper function
using namespace std;

void showmenu(); // function prototype

int main()
{
const int SIZEMAX = 40; // defining array size for files names
char filename[SIZEMAX];
char outname[SIZEMAX];
char answer;
int option_choice;
char ch; // to hold character
ifstream in_File; // input mode variable declared
ofstream out_File; // output mode variable declared

do
{
// get input file name
cout << "Please enter input file (read from):\n"; // dont forget to add the .txt
cin >> filename;
// open file for input
in_File.open(filename);
// get output file, output mode declared
cout << "Please enter output file name (write to):\n";
cin >> outname;
//open file for output
out_File.open(outname);
//verification of input file
if (!in_File)
{
cout<< "Error. Cannot open file " << filename << endl;
system ("pause"); // terminates the program immediately
return 0;
}

cout << "Please choose one of the following option choice numbers:\n";
showmenu(); // function call
cin >> option_choice;

if (option_choice == 1) // UPPERcase conversion
{ // Process files
in_File.get(ch);// get a char from file 1
while (!in_File.eof()) // verification test eof
{
out_File.put(toupper(ch)); // write uppercase char to file 2
in_File.get(ch); // get another char from file 1
}
//close files
in_File.close();
out_File.close();
cout << "File conversion done.\n";
cout << "Please check output file now. Thanks\n";
cout << "Would you like to go again. (Y/N)\n";
cin >> answer;
}

if (option_choice == 2) // LOWERcase conversion
{ // Process files
in_File.get(ch);// get a char from file 1
while (!in_File.eof()) // verification test eof
{
out_File.put(tolower(ch)); // write lowerercase char to file 2
in_File.get(ch); // get another char from file 1
}
//close files
in_File.close();
out_File.close();
cout << "File conversion done.\n";
cout << "Please check output file now. Thanks\n";
cout << "Would you like to go again. (Y/N)\n";
cin >> answer;
}

if(option_choice == 3)
{
}

if (option_choice == 4) // Quit option
{
cout << "You have quit the program.\n";
system ("pause");
return 0;
}
}while (answer == 'y');


system ("pause");
return 0;
}

///////////////////////////function defintions//////////////////////////////////

void showmenu()
{
cout << endl;
cout << "-----------------------------------------------------\n";
cout << "\t\tMENU FILTERS \n";
cout << "-----------------------------------------------------\n";
cout << "1. Convert all alphabetic characters to uppercase" << endl;
cout << "2. Convert all alphabetic charcaters to lowercase" << endl;
cout << "3. Replace all numeric(digit) charcaters with spaces" << endl;
cout << "4. Quit\n" << endl;
}



Last edited on
Topic archived. No new replies allowed.