When I enter an incorrect filename the program terminates, but doesn't display the cout line. When I step thru in VisualStudio 2010, it shows the couts displaying correctly. What am I overlooking?
// Request file name from user
cout << " Please input the filename and path: ";
cin >> fileName;
inFile.open(fileName);
if(!inFile)
{
cout<< "Cannot open input file. "
<< "Program ending. " << endl;
return 1;
}
I'm a student- so a hint or psuedo code would be very helpful.
Thanks!
This will work fine if you're running Windows OS. People don't like it here, but for homework or using it just for messing around it's completely acceptable. I'm surprised you're working on file input and output and don't know about system pausing. That is one of the last pieces of information I learned in my courses. We learned system("pause"); on day one with "Hello World!"
@Computergeek01: I don't even know what you mean. He just wants to pause his screen. My code above has been edited, it was backwards. I put the return 1; instead.
What he means is that you/he can make a class that has the pause-ish code in the destructor, then simply make an object at the start of the program. Then, no matter how or where the program stops, the destructor is always run and the pause-ish code can keep the window open.
I was actually playing around with the idea of using a function attribute to declare a pause function as the destructor to the entire program (this is what I do and it works wonderfully), thus eliminating the need for an object. For GCC\MingW this is simply:
1 2 3 4 5
void pause() __attribute__((destructor));//Attributes cannot be assigned in the definition
void pause()
{std::cin.sync();
std::cin.ignore;}
But these are compiler specific and I'm still looking for the documention on the cl compiler.
EDIT: Although LB's solution is NOT compiler specific, a little simpler and 100% valid.
@Khaltazar- That did it! I put the system pause in just above the return 1 line in the if statement, and it works fine. So MANY THANKS!
Our teacher told us to include a system ("pause") line at the end of the file so info would stay on screen, but we were not told how it works. I had it there, but in the wrong place.
Thanks SO MUCH. I'll consider some of these more elegant solutions as I learn.
As for how it works, the "system(...)" function is part of cstdlib.h and just passes whatever string you pass as an argument up the chain to the shell, in this case cmd.exe. One major reason we don't like it used to hold a program open is because specifically when cmd.exe is passed the "pause" command it opens an entire second shell within the memory space of the application. This is slow, wasteful and unnecessary when you could just use the std::cin stream.
I'll be the first to admit that saying "system() is bad because it isn't cross platform" is a redundent and bullshit argument. The whole purpose of "system(...)" is to pass commands to the shell so of course it's platform specific. Also, "system()" does have good uses but pausing the console is NOT one of them.