+ 1

C++ python functions. Bless or mess?

So I am kinda new to C++ and recently learnt functions. I never really liked the "cout" and "cin" statements and always keep forgetting, should I use << or >>. So i decided to make a function that works as simple as "print" in python. So the question is, is it a good idea to use these simple functions or am I just being stupidly lazy? And also how can i make input() (and other simple functions) work, if I at all should? Code: https://code.sololearn.com/c7j8893aiO6X/?ref=app (Sorry for bad code).

20th Aug 2022, 7:23 PM
Tukk0
Tukk0 - avatar
4 Respuestas
+ 3
Your Functions are for your understandings.. So you have to decide about yourself about the function... But for others its a mess. What if type of data is not int? Are you going overload functions?
20th Aug 2022, 7:54 PM
Jayakrishna 🇮🇳
+ 1
I often prefer printf() to cout<< bc it's shorter and easier to control the width of the displayed fields for nice alignment, i.e.: printf("x = %03d\n", x); vs: cout << "x = " << setw(3) << x << '\n'; But I like how cout<< displays floating point numbers. What helped me not confuse << and >> was to realize that cout and cin are objects that have overloaded the shift left << and shift right >> operators. cout is an output stream, so it has << towards it, bc you send things to it, cin is an input stream and has >> towards the variables that receive from it.
21st Aug 2022, 2:45 AM
lion
lion - avatar
+ 1
Thank you everyone for anwsering, I got the point now.
21st Aug 2022, 8:34 AM
Tukk0
Tukk0 - avatar
+ 1
input() from python always returns a string, so it should be easier to implement: string input() { string s; getline(cin, s); return s; } You'll have to use C++ specific functions to convert it to different types though: int i = stoi(input()); double x = stod(input()); like in this example: https://code.sololearn.com/cKZd1zNc7Xe4 I also found here https://gist.github.com/ilebedie/f006674098a1adaab731 an example of python style print() that accepts multiple arguments, but idk to explain it: https://code.sololearn.com/co4GYHTim6VU Btw, you'll have to provide input when you run it here on SL and it has to be on two lines, smth like this: 123 Hello
21st Aug 2022, 9:48 AM
lion
lion - avatar