+ 1
How we can show output without using cout in c++?
I'm new for sololearn. plz help me.
8 Answers
+ 8
You can use printf function.
http://www.cplusplus.com/reference/cstdio/printf/
You will need to include <cstdio> header.
NOTE: if you are printing value of std::string instead of C-String (char array) you will have to use the value from <string object>.data(), or <string object>.c_str().
Example:
#include <string>
#include <cstdio>
int main()
{
std::string sol {"SoloLearn"};
const char *com {"Community"};
int year {2018};
printf("%s %s %d", sol.c_str(), com, year);
return 0;
}
Hth, cmiiw
+ 5
Assuming the intent of the question is "write something to screen", then along with Ipang 's explanation which has some C flavor, there are two other ostream objects which are tied to standard error stream (stderr) -- which is a different stream than stdout -- and somehow capable of handling the output-to-screen needs and we can abuse them shamelessly!
1. std::cerr
Usually writes to the same device as the standard output -- Same as std::cout. By default, writings to std::cerr are not buffered -- which means it's slower than std::cout -- and for the most part "used for error message or other output that is not part of the normal logic of the program".
2. std::clog
More like std::cout -- which means the stream is buffered -- and as the name suggests, is used to report program's information to a log file or screen.
+ 4
Åerban Daniela-Mihaela LOL!
NOTHING!
NOTHING!
NOTHING!
I guess I've been encountered this question for 1,001 times, here!
It gets implicitly defined for you. So don't worry about it anymore, OK?! ;)
+ 3
thanks for your valuable answer
+ 2
You're very welcome : )
+ 2
Hi. Somebody can tell me what happens if i don't use the return 0? I tried and is the same result. Many thanks. :)
+ 1
Printf
0
You may use
printf ("example");