-3

I encountered this problem : https://www.urionlinejudge.com.br/judge/en/problems/view/1193

    /*input
3
101 bin
101 dec
8f hex
*/
/*************************************************************
 * Purpose : https://www.urionlinejudge.com.br/judge/en/problems/view/1193

 * Author: Sahil Arora

 * Version: 1.0

 * Date: 22/10/15

 * Bugs : None

*************************************************************/
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
    /* code */
    std::ios_base::sync_with_stdio(false);
    int test;
    cin>>test;
    for(int i=1;i<=test;++i){
        cout<<"Case "<<i<<":\n";
        string str,base;
        long long num,j;
        cin>>str>>base;
        if(base=="dec"){
            num = stoll(str,nullptr);
            cout<<hex<<num<<" hex\n";
            bitset<32> bin(num);
            for(j=31;bin[j]==0 && j>=0;--j)
                ;
            while(j>=0)
                cout<<bin[j--];
            cout<<" bin\n\n";
        }
        else if(base=="hex"){
            str = "0x" + str;
            num = stoll(str,nullptr,16);
            cout<<dec<<num<<" dec\n";                   // <--focus on this line
            bitset<32> bin(num);
            for(j=31;bin[j]==0 && j>=0;--j)
                ;
            while(j>=0)
                cout<<bin[j--];
            cout<<" bin\n\n";
        }
        else{
            num = stoll(str,nullptr,2);
            cout<<dec<<num<<" dec\n"<<hex<<num<<" hex\n\n";     
        }   
    }
    return 0;
}

Now on changing line 45 to :

cout<<num<<" dec\n";

My output for hex changes. It gives the same output as input for hex to dec.I fail to understand why it gives such an error. Also, if I enter only 1 test case, it gives correct output for a hex, but still gives 20% Wrong answer on submission!

Input :

3
101 bin
101 dec
8f hex

Expected output :

Case 1:
5 dec
5 hex

Case 2:
65 hex
1100101 bin

Case 3:
143 dec
10001111 bin

My Output without using dec in cout :

Case 1:
5 dec
5 hex

Case 2:
65 hex
1100101 bin

Case 3:
8f dec
10001111 bin
Sahil Arora
  • 875
  • 2
  • 8
  • 27
  • `stoll` is **not** a C++ function, but a good old c library fuunction. – Marcus Müller Oct 23 '15 at 09:18
  • You really need that much code to testcase the behaviour of `stoll()`? And what's with `#include`...?!? – DevSolar Oct 23 '15 at 09:21
  • @MarcusMüller I guess stoll was introduced in C++11 in string, not in C – Sahil Arora Oct 23 '15 at 09:24
  • @DevSolar Not actually testcasing the behavior of `stoll`, but tried using it, and couldn't figure out the error. And the `#include` was to omit adding all different headers. – Sahil Arora Oct 23 '15 at 09:27
  • 1
    @MarcusMüller, http://en.cppreference.com/w/cpp/string/basic_string/stol – Lol4t0 Oct 23 '15 at 09:31
  • 1
    Could you please provide desired and actual input and output because it is not clear what is wrong exactly – Lol4t0 Oct 23 '15 at 09:36
  • @Lol4t0, Sahil: OMG. C++11 is crazy. Please excuse my mistake. I confused it with C89's `atoll` which basically does the same -- in most environments, since about 30 years. I just couldn't imagine they'd include a function like that *now*. – Marcus Müller Oct 23 '15 at 09:37
  • @Lol4t0 Edited, thanks for suggestions! – Sahil Arora Oct 23 '15 at 09:49
  • Downvoter, can you please explain the reason for the downvote? – Sahil Arora Oct 23 '15 at 10:25
  • @SahilArora: Not my downvote, but I still don't like your "include shortcut" there. For the benefit of saving two or three lines of standard `#include`s, you limited your program to libstdc++ plattforms by making it non-conforming. Also, another case where reducing the scope (and size) of the example code would very likely have resulted in you figuring it out on yourself. – DevSolar Oct 23 '15 at 10:50
  • @DevSolar: It saves a lot of time when you are into competitive programming, though I did not know that it limits your platform and increases compile time, but isn't that harmful either. Have a look : https://www.quora.com/Is-it-good-practice-to-use-include-bits-stdc++-h-in-programming-contests-instead-of-listing-a-lot-of-includes Thanks for correcting! – Sahil Arora Oct 23 '15 at 11:53
  • @SahilArora: The only "competitive" habit I'll be engaging in is making my software clear, portable, maintainable, and as bug-free as possible. But it explains your aversion to whitespaces as well. – DevSolar Oct 23 '15 at 12:26
  • @devsolar : I am a starter at competitive programming, and am trying to make my code as clean as possible, but still learning. Can you suggest edits to make it better? And I didn't get the thing about aversions. – Sahil Arora Oct 23 '15 at 12:31
  • Your code sample does not compile, please edit to use actual C++ includes. – Cubbi Oct 23 '15 at 18:28

1 Answers1

2

I believe your program behavior is expectional and has nothing to do with stoll itself.

So, std::hex, std::dec (and std::oct) are manipulators that modify output base. Once applied they will not reset (this changes them from std::setw for example). That is why you should reapply manipulators every time you want to change output base.

So you just have to write

std::cout<<std::dec<<num<<" dec\n"; 

because you may have applied

std::cout<<std::hex<<num<<" hex\n";

earlier

Lol4t0
  • 12,444
  • 4
  • 29
  • 65