[C++] A very basic while loop problem...

Slightdust

Member
Sep 18, 2005
172
0
0
I just got into C++ programming, and I'm stuck on while loop.

Take a look at the code.

http://img153.imageshack.us/img153/317/codeseg5.jpg

Where I labeled "PROBLEM RIGHT HERE!!!!!"? I want the program to loop back to the top...

So basically what I'm trying to achieve here is for the program to start from beginning (where user type in the file name) IF the file does not exist or is invalid. And if the file does exist and is valid, then it go on to display the data.

Help?
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
This code has no loop in it.

pseudo code:

...

for ( ;; )
{
message("enter name")
get(filename)
open(filename)
if (! fail) break;
message("can't access", filename)
}

...
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I don't see any while loop in there... which could be the problem. Although the central if block is strangely indented as if something were missing.

Anyway... what's your termination condition? Whatever it is your loop looks like this...

while (!termination_condition)
{
// do stuff here and eventually set termination condition
}

Look at the block of logic that you want to repeat: in this case you output the text "Try again..." but you don't prompt the user for that choice, and you don't have any looping structure. If the block you want to repeat is the get filname, try file, report results block, then you need to enclose that block in a while loop that tests an exit condition such as a boolean flag. If the file read succeeds you can set the boolean to true and drop out of the loop. If the read fails you prompt the user, and if they decide not to try again you also set the flag to true and drop out of the loop. Otherwise you loop back to the top and prompt for the input file again.

Some pseudo code:

set exit is false
while exit is false
**get filename
**try to open file
**if open succeeded
****read data
****set exit is true
**else
****prompt user
****if user says no
******set exit is true
****end if
**end if
end while
 

Slightdust

Member
Sep 18, 2005
172
0
0
Take a look at the new code in that segment:

while(!inFile)
{
cout << "Input File with name: " << iFile << " could not be opened\n";
cout << "Try again..\n" << endl;
cout << "Enter the name of the input file (ctrl-c to exit): ";
cin >> iFile;
}

But the problem with that is that after typing in one invalid file name, it will never quit the loop even if a valid file name is entered...It will still show it as it's an invalid file.
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
variable declarations...

xxx while(!inFile)
for ( ;; )
{
cout << "Enter the name of the input file (ctrl-c to exit): ";
cin >> iFile;

inFile.open(iFile.c_str());
if (inFile) break; //out of infinite for loop

cout << "Input File with name: " << iFile << " could not be opened\n";
cout << "Try again..\n" << endl;
}

read...
output...
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Which part/lines?

01variable declarations...
02
03 xxx while(!inFile)
04 for ( ;; )
05 {
06 cout << "Enter the name of the input file (ctrl-c to exit): ";
07 cin >> iFile;
08
09 inFile.open(iFile.c_str());
10 if (inFile) break; //out of infinite for loop
11
12 inFile.clear();
13 cout << "Input File with name: " << iFile << " could not be opened\n";
14 cout << "Try again..\n" << endl;
15 }
16
17 read...
18 output...

---

added line 12.