Problem with file and loop

Mar 1, 2011 at 5:58pm
edit.
Last edited on Mar 17, 2011 at 1:09pm
Mar 1, 2011 at 6:31pm
You can probably just put an "&& !myInput.eof()". This will test if the file has been processed completely after your last "myInput >> c;".

Or it looks like you could additionally put a 'q' as your last instruction in the file in order to exit the program.
Mar 1, 2011 at 6:34pm
I've tried "&& !myInput.eof()" and it did solve the problem, but it forces the program to quit since the condition is no longer true. I would like it to have the same effect but essentially let the user manually enter the commands afterward
Mar 1, 2011 at 6:49pm
use the current while loop to process the file (you will use the eof() condition for that), then afterwards, use another while loop to keep asking the user for input to move the robot, or a value to terminate the loop and thus terminate the program.

make sense?
Last edited on Mar 1, 2011 at 6:58pm
Mar 1, 2011 at 6:54pm
Don't loop on eof(). You don't want to try to read from a stream if it's in one of three states: eof, fail or bad. The name of the stream itself will evaluate to false if it's in one of those states, so you should just say

1
2
3
while(stream) {
    // ...
}

or

1
2
3
while(stream >> var) {
    // ...
}
Mar 1, 2011 at 7:04pm
@tubagreg - That was my initial thought to fix the program, but I didn't want to copy the while loop over and have excess code. I assumed there was another way. If not than please tell me.

@filipe - Can you elaborate on "try to read from a stream"? Like I said, I've only been studying C++ for about 1 month and just learned how to i/o files today. I haven't even learned fail or bad, even though I've read about them outside of school.

Can you explain "stream >> var" further?

Mar 1, 2011 at 7:15pm
I'm very new to C++ myself so forgive me if I don't know what I'm talking about, but I think you could use an if statement in your main function to do what you're trying to do.

1
2
3
4
5
case 'Q':
                    case 'q':
                    cout << "You have quit the program." << endl;
                    q = true;
                    return 0; //return to main instead of breaking 


Then somewhere in main:

1
2
if (q == true){
                      return 0;}


This would probably only work if q is a constant?
Mar 1, 2011 at 7:27pm
@Rofflecopters - I don't think I need another switch statement because all my cases works. The problem is after the file is open & read the program finishes. I DONT want it to finish and enable the user to move the robot manually.
Mar 1, 2011 at 7:38pm
mookial wrote:
Can you elaborate on "try to read from a stream"?

This is an attempt to read from a stream:

1
2
int a;
std::cin >> a;

or

1
2
3
int a;
ifstream ifs("file.txt");
ifs >> a;


So when I said stream >> var I just meant any type of input stream >> any type of variable.
Mar 1, 2011 at 7:48pm
@filipe - Thanks for the tip. I changed the while loop to while ( q == false && myInput >> c){ but now it won't read all the commands in the .txt file. :(

P.S Is the problem I'm facing not solvable without adding a copy of the while loop without the
if (myInput.is_open()) to start it off?
Last edited on Mar 1, 2011 at 7:53pm
Mar 1, 2011 at 7:56pm
Sorry - I misinterpreted your problem.

Would adding this to main work?

1
2
3
while (q == false){
/*pointer or call to your code*/;
}
Mar 1, 2011 at 8:14pm
@rofflecopters - i already have that above
Mar 1, 2011 at 8:21pm
Ok. I skimmed through the previous posts and it seems you want the program to read from a file and then ask for user input. So this is what you want:

open file
    read from file
    close file
read from keyboard

Right?

Reading from a file or from user input are both specific instances of something more general: reading from an input stream. You probably don't know classes and inheritance yet, but keep this in mind: cin and an ifstream object (like the ifs object in my example above) are both istreams (which is the type for input streams).

This means that, instead of handling user input and file reading as two different things, you'll want to handle any kind of istream. We accomplish this by defining a function that will read from istreams for us:

1
2
3
4
5
6
7
8
9
10
11
void read(istream& is)
{
    while(true) {
        // ...
        char c;
        /* the following line tries to read from is;
           if it succeeds, execution goes on; else it returns */
        if(!(is >> c)) return;
        // the rest of your code (case 'q' needs to return, though)
    }
}

We could use such a function like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream ifs("file.txt");
    if(ifs.is_open()) {
        read(ifs);
    }
    else {
        // you might want to deal with the error here
    }
    read(std::cin);
}

Be warned that I didn't test anything, but that's the gist of it.
Last edited on Mar 1, 2011 at 8:22pm
Mar 1, 2011 at 8:34pm
edit,
Last edited on Mar 17, 2011 at 1:08pm
Mar 2, 2011 at 2:57am
Anyone care to help?
Mar 3, 2011 at 3:32am
bump
Topic archived. No new replies allowed.