looping

closed account (4Gb4jE8b)
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
	}
}
why wouldn't recursion or any of the built in loops work?
closed account (4Gb4jE8b)
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'm a little confused at what you're trying to do...
but Would this work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(void){
	//declarations
	bool running = true;
	do{
		if(Parent_file.rfind(".txt") != Parent_file.length() - 4){
			cout << "File must be a .txt\n";
			pause();
			running = true;
		}
		if(fexists(Parent_file.c_str()) == false){
			cout << "No file exists of that name\n";
			pause();
			running = true; 
		}
		//statements
		running = false;
	}while(running);
}
closed account (4Gb4jE8b)
yeah actually it would, it'll be annoying to implement with 300 lines of code but hey, it happens. Thanks for your help!
no problem :)
closed account (4Gb4jE8b)
no i lied. Sorry, I want it to loop back to the beginning of the do statement at those points. Do you see what i'm saying?
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.
closed account (4Gb4jE8b)
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
Last edited on
closed account (4Gb4jE8b)
you know i didn't think about using a do while specifically around the if statement. Thanks intrexa, i'll get back to you on whether or not it works
Topic archived. No new replies allowed.