there is an if statement i have, where i want it, if it is met, to loop to the beginning of the program without using a for or while loop (as they are really inapplicable), and without using a goto statement, and without calling main(). Is there any process of doing so?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
//declarations
if(Parent_file.rfind(".txt") != Parent_file.length() - 4)
{
cout << "File must be a .txt\n";
pause();
return 0; //i want it to loop here instead of closing
}
if(fexists(Parent_file.c_str()) == false)
{
cout << "No file exists of that name\n";
pause();
return 0; //i want it to loop here as well
}
}
what would i run it against? For example, how would i integrate this into a for loop that would loop the original statements as long as these if statements were true?
I do, I'm just trying to think of how to implement it, I'm thinking switch case, but every implementation that comes to mine using this seems a little rube goldberg.
that's kind of been my problem. And also remember that like i said, it's over 300 lines of code (it could be much worse, but all external functions are held in other cpp files), and there are multiple situations like this (it's error checking after all)
I'm not 100% entirely sure what it is you are trying to do. As such, my answer may not be 100% applicable. It is my understanding that the user enters a specific file, then you test to see if that file is a valid file, if not, you prompt the user to enter another file. As such,
1 2 3
do{
File = GetFile(); //make a function that returns a file
}while(!VerifyValidFile(File)); //function that verifies that it is the file you are looking for(all the code you have in your current main, basically)
That code would prompt to get a file, then if it is not a valid file (valid by what you specify) prompt the user to enter another file. You would need someway to escape if the user cannot enter a valid file(no valid file exists)
If my understanding of your problem is incorrect, please let me know how what you need differs from the sample I provided
edit: line 3 of the code, it should loop if NOT a valid file. If the VerifyValidFile(File) function logically returns true when it is a valid file, you would need to inverse it to make it loop properly