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
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.
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
@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.
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
@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.
@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?
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.