top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why can't output entire stream in c++?

+1 vote
278 views

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;
}
posted May 1, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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";
}
answer May 1, 2016 by Rajan Paswan
...