Why can't I output entire stream in following c++ program?
#include <iostream> #include <stdlib.h> int main() { std::cout << "Welcome " << getenv("USERNAME") << " to APP_NAME. To get available commands use --help" << std::endl; return 0; }
You should test the return value before using it.
#include <cstdlib> #include <iostream> int main() { if (const auto user = std::getenv("USERNAME")) std::cout << "Hello, " << user << "\n"; else std::cout << "Hello, whoever you may be.\n"; }