Ludovic 'Archivist'
Should i use it aswell?
Remember all your Most Vexing Parse issues in the past?
olli
Because its ugly, redundant, and unclear of intent
I thinks it's neither of those
Ludovic 'Archivist'
Oh yea
= is assignment {} is initialization
olli
Item 7 in Effective Modern C++ might be a good read. To summarize: Braced initialization is the most widely usable syntax, also preventing narrowing and being immune to most vexing parse, one need to be careful if a constructor overload exists that takes a std::initializer_list Imho it's a great book to read about best practices in c++11/14
Alignant
= is assignment {} is initialization
It's more logical to use parentheses for initialization.
◔͜͡◔ ทa
Mr
Try it.. I given question already
◔͜͡◔ ทa
When I Finnish doing it should I post ot its just for me
Ludovic 'Archivist'
It's more logical to use parentheses for initialization.
You think so? Then what does this do? std::vector a(0);
Mr
When I Finnish doing it should I post ot its just for me
If i input -0 then output will be negative zero If i input 0 then output will be zero
olli
the logic can be done in like 3 lines
D
Still not getting output
Take it in as a string and check the first index. int main(){
D
char blah[]: fgets (blah); if (blah[0] == "-"){ }
D
else { }
D
can you show us your code so far?
Put in your solution here brother, that was a better one !!! :)
olli
void fn(const std::string& in) { try { const int num = std::stoi(in); std::cout << (in[0] == '-' ? "negative " : "positive "); std::cout << (num == 0 ? "zero" : "number") << "\n"; } catch (...) { std::cerr << "Invalid input\n"; } }
Mr
can you show us your code so far?
I used if else condition
D
I used if else condition
even he has used if else .. Just a shorter version
D
google this notation for it —> Condition ? True : False
Mr
Take it in as a string and check the first index. int main(){
If u input -8 then output will b negative number Sir
olli
I used if else condition
using the "ternary operator" ?: is shorter. You can also use if and else though if(in[0] == '-') { std::cout << "negative "; } else { std::cout << "positive "; } ...
D
Array Why u used?
where are you going yo take the string ? You can use a char* String, but then need to use malloc !!
Mr
I am little it confused
Mr
Can u send full code
Mr
I will try in compiler
D
Array Why u used?
YEp try it out on -8, it wil work !!! Don't worry, thats just psuedo code.. For real soln, check @ollirz Solution.. That's like produciton ready stuff !! :P
Mr
Send me full code i will try..
D
Send me full code i will try..
That's the full code.. If you can not get the full code from @ollirz solution, then you should better get a class of programming first !!!
D
Send me full code i will try..
Please give it a try before asking for solutions !! That is not cool nor helpful !!!
Mr
Mr
I just tried this In java... And i used same has c
D
Dude... you have used INTEGER ... use fucking STRINGS man!! Check all the soln above !!!
Mat
n == -0 will always be equals to n == 0
olli
using integral values there is no difference between '0' and '-0'. (Floating values however differentiate between 0 and -0 since their sign is stored in a dedicated bit.)
Mr
n == -0 will always be equals to n == 0
Yup that's the reason i want
olli
where?
Mr
Google
olli
Please elaborate. Your code does not make use of any ascii value comparison
Mr
D
n == -0 will always be equals to n == 0
Should not be the case with signed integers AFAIK !!! @Mr_vabs try signed integers !!
D
https://en.wikipedia.org/wiki/Signed_number_representations#Signed_magnitude_representation
olli
Most systems make use of the two's complement. There are proposals to define this in the C++ standard as well. (Currently the layout of signed integral values is not defined for C++)
Ludovic 'Archivist'
Should not be the case with signed integers AFAIK !!! @Mr_vabs try signed integers !!
There is no negative 0 in most integer representations, only on IEEE float ones
olli
However I am currently unaware of any consumer platform that does *not* use two's complement
Ludovic 'Archivist'
D
Well a defacto one
Yep. Thanks.. I had kind of strayed .. Rusty memory :P
Ludovic 'Archivist'
Mainly because unsigned integers in C are defined in a way that makes it suitable to be used like this
Ludovic 'Archivist'
In short, because unsigned integers are defined in a ring-ish way, defining signed ones to rely on this behavior is cheap
olli
You can find the solution above
Ludovic 'Archivist'
This is possible or not?
Using floats, yes
olli
Using floats, yes
or by inspecting the input as string
Ludovic 'Archivist'
Code please
C or C++?
Mr
Any
Mat
Someone already posted the code
Dima
lol lazy newbs
Mr
lol lazy newbs
I tried alot 2 days
Dima
try more
Dima
thats the point
olli
Full code..
#include <iostream> #include <string> void fn(const std::string& in) { try { const int num = std::stoi(in); std::cout << (in[0] == '-' ? "negative " : "positive "); std::cout << (num == 0 ? "zero" : "number") << "\n"; } catch (...) { std::cerr << "Invalid input\n"; } } int main(int, char**) { std::string input; std::cin >> input; fn(input); return 0; }
Mat
I tried alot 2 days
Not in the right direction
olli
I expected you'd be able to read a string and pass it to a function
Mat
Please, try to learn something more basic
D
*your