Hello, I am writing a short program to implement a shell, and I am having an unusual problem. For some reason, I cannot clear the std :: cout buffer. The program does not print messages. I understand that a simple solution is to switch to std :: cerr, but is there a way to get messages to print using cout? Things I tried:
std::cout.flush()- Insert
std::endlafter everything is written to standard. - Insertion
std::flushin the output stream std::cout.setf(std::ios::unitbuf);I found that I should output unbuffer output.
Any help is much appreciated here, this is my code:
int main()
{
std::cout.setf(std::ios::unitbuf);
std::string input;
std::cout << "myshell> ";
std::getline(std::cin, input);
char * buf = new char[input.length() + 1];
strcpy(buf, input.c_str());
int index = 0;
char * command[256];
command[index] = std::strtok(buf, " ");
std::cout << command[index] << std::endl;
while (command[index] != NULL)
{
++index;
command[index] = std::strtok(NULL," ");
std::cout << command[index] << std::endl;
}
std::cout.flush();
std::cout << index << " items were added to the command array" << std::endl;
delete[] buf;
return 0;
}
source
share