I know there are similar questions about EOF and CTRL+D, but I have a more specific question. The way I understand it, CTRL+D signals the end of STDIN and is handled by the computer and not the running application. However, I need to provide feedback to the user of my program on an EOF character typed via (CTRL+D) or contained within an input file containing commands. How would I do this?
I have included my simple code on how I thought to do it, but it doesn't work for obvious reasons:
#include <iostream>
#include <string>
using namespace std;
int input()
{
string cmdstring;
cin >> cmdstring;
if (cmdstring == "bye") //Exit on "bye" command
{
return 1;
}
else if (cmdstring == "^D") //Exit on EOF character CTRL+D
{
return 2;
}
else //Continue shell prompt
{
return 0;
}
}
I'm trying to write my own shell, and I want to provide an exit status when the shell quits. Thank you very much!
Edit: I changed it to cin.eof(), but it still doesn't work.
else if (cin.eof()) //Exit on EOF character CTRL+D
{
return 2;
}
Additionally, I forgot to mention that this code is a function running within a loop, so the user is continually prompted until they supply "bye" or an EOF character is read.
int exitstatus = 0; //Tracks exit code status
do {
exitstatus = input();
} while (exitstatus == 0);