I use a while loop "while ((isValid(my_file, file_name) == false))" to call a separate function isValid to check and see if it is valid and it works just fine in every situation except when I follow an invalid file with a valid and I have tried clearing it and it does not work and I cannot figure out what to do... Here is my code, I only included the main function and the isValid function the others are not important for this problem as far as I know.
//This function when called by main function will determine if file is valid
//It will do this by checking to make sure it ends in 'htm' and that the file exists
//and return true if it is a good file name and false if it is not.
//call library header file
#include "Libraries.h"
//declare function that will determine if file entered exists and is an html file using and ifstream and string parameter
bool isValid(ifstream& file, string name)
{
//use if statement to determine if file entered ends in 'htm' and if so it will return false
//also will return false if file name entered does not even consist of 4 characters
if ( (name.length() < 4) || (name.substr(name.length() - 4)) != (".htm"))
{
return false;
}
// another if statement that verifies file exists and if it does it returns true
if (file.good())
{
return true;
}
// returns false if file does not exist
else
{
return false;
}
//end function and header file
}
int main()
{
//declare variable
string file_name;
//start infinite loop
while (true)
{
//ask user for title and then store it in string and then change it to an ifstream
cout <<"please enter a HTML file name or hit 'exit' to quit and " << endl;
cout << "if you want to clear file please enter 'clear': ";
getline(cin,file_name);
cout << "" << endl;
ifstream my_file(file_name.c_str());
//use if statement to determine if name entered was exit and if it was break out of infinite loop and end program
if(file_name == "exit")
{
break;
}
//Use another if statement and it clear is entered call clear_file function and it will
// clear file, then notify you, and then continue back to top of loop
if(file_name == "clear")
{
Clear_file();
cout<< "You have just cleared you text file 'titles.txt'" << endl;
cout <<"You may now continue running your program until you enter 'exit'." << endl;
cout << "" << endl;
continue;
}
//Use loop to call function isValid to see if it returned true or false, if false it will enter loop
while ((isValid(my_file, file_name) == false))
{
//file_name.erase();
//tell user file name was invalid and ask for another one amd keep asking and calling function to check if valid until valid name is given
// and when one is store in string and convert to ifstream and exit loop
cout <<"Invalid file name(must be an HTML(.htm)) file and it must exist." << endl;
cout << "please enter a VALID file name: ";
getline(cin,file_name);
cout << "" << endl;
//ifstream my_file(file_name.c_str());
}
// when it returns true call function to open file and then call output function(which in return will call another function to get
// title of file and then the output function with output it to a text file)
Open_file(file_name);
Output_function(my_file);
//close file when done and continue through loop until over and over again the word 'exit is entered.
my_file.close();
}
//once user enters exit notify them that the program is done and all of the titles are stored in a separate file unless the cleared it
cout << "" << endl;
cout <<"Thank you for using this program! " << endl;
cout << "Your titles are all stored in the file 'titles.txt', " << endl;
cout << "unless you cleared that file. Have a nice day!!" << endl;
cout << "" << endl;
//pause and end program
system("Pause");
return 0;
}

New Topic/Question
Reply


MultiQuote




|