C++ wft?



  • I was in my c++ class today and my teacher was showing us how to get an integer out of a character from a string, and it smelled fishy.

    d += (bnum.at[i] - '0') * p;

    That was what we had to do, where bnum was a string that held a binary number in string format (after all, a binary number might be larger than an int can hold). This just smells really fishy to me. Anyone care to enlighten me as to the proper way? 



  • int i = 0; total = 0;

    while (i < bnum.length())

    {

        total = (bnum.at(i) - '0') + (total * 10);

        i++;

    }

     

    that might be what you want.  im not sure i understand the full extent of the problem.



  • What do you think is wrong with that? Out of context, there's no apparent problem.



  • I presume p is equal to 2^i and that this is inside a for loop.  In which case it's perfectly acceptable, if not optimal.



  • @bstorer said:

    I presume p is equal to 2^i and that this is inside a for loop.  In which case it's perfectly acceptable, if not optimal.

     

    2 << i would be optimal.



  • ok after actually reading the original post and (hopefully) understanding it correctly:
     

    unsigned int conversion(const string& bnum)
    {
      unsigned int total = 0;
      unsigned int addend = 1;

      for (int i = bnum.length() - 1; i >= 0; i--)
      {
        total += (bnum.at(i) - '0' ? addend : 0);
        addend <<= 1;
      }
      return total;
    }

     

    works for me.  this is also assuming your binary number is unsigned and an integer.  oh and if you dont like ternary shutup i do. 




  • int atoi(const string & num) { return atoi(num.c_str()); }

    -- or --
    int atoi(const string & num) {
    int ret = 0;
    string::size_t len = num.length(); // if not it's size or count
    for (string::size_t i = 0; i < len; i++) {
    if (is_numeric(num[i])) { //Note .at() is the same as []
    ret += (num[i] - '0'); // What's with the * P in the OP?
    }else {
    ret = 0; break; errno = WHATEVER_THE_STANDARD_USES;
    }
    }
    return num;
    }



  • Incidentally, has anyone else notice that the Subject is "C++ wft" (emphasis mine)?  Is that not, truly, the Real WTF?



  • @bstorer said:

    Incidentally, has anyone else notice that the Subject is "C++ wft" (emphasis mine)?  Is that not, truly, the Real WTF?

    If not, then surely the Real WFT. 



  • What Fa Tuck?



  • The wtf that I think was about the casting of the integer to a character or string. My instructor said we [i]had[/i] to add 0 to it to convert it to a character or string, that there was no other way.



  • @malfist said:

    The wtf that I think was about the casting of the integer to a character or string. My instructor said we [i]had[/i] to add 0 to it to convert it to a character or string, that there was no other way.

    Why would any sane person expect int num = (int)'3'; to set num as 3? Furthmore why does it do such a bizzare thing? It's like asking a bug to happen.



  • @Lingerance said:

    @malfist said:
    The wtf that I think was about the casting of the integer to a character or string. My instructor said we [i]had[/i] to add 0 to it to convert it to a character or string, that there was no other way.

    Why would any sane person expect int num = (int)'3'; to set num as 3? Furthmore why does it do such a bizzare thing? It's like asking a bug to happen.

    It doesn't. It sets it to 51. 



  • i dont see any casting in the original post though.  however, if you want to get the value of the character '3', its '0' + 3 (or just '3'), unless you want to memorize your ascii tables.


     



  • @segmentation fault said:

    i dont see any casting in the original post though.  however, if you want to get the value of the character '3', its '0' + 3 (or just '3'), unless you want to memorize your ascii tables.

    All casting in the original post is implicit. 



  • like i said, i dont "see" any casting 😉

     

    if you have a character which you know is a representation of a digit '0' through '9', subtracting '0' from it is the best way to get the value which it represents.

     

    im still confused though.  is this string supposed to be a number in base 10 or base 2?



  • @segmentation fault said:

    like i said, i dont "see" any casting 😉

     
    if you have a character which you know is a representation of a digit '0' through '9', subtracting '0' from it is the best way to get the value which it represents.

     im still confused though.  is this string supposed to be a number in base 10 or base 2?

    @malfist said:

    where bnum was a string that held a binary number in string format



  • strtoul(s,NULL,2)?



  • Why are you learning C stuff in a C++ class?



  • @Daid said:

    Why are you learning C stuff in a C++ class?

    strings are not C. 

    @Obfuscator said:

    strtoul(s,NULL,2)?

    that will work if you pass bnum.c_str(), but im guessing this is a typical college class in which the teacher wants you to actually learn whats going on by having you reinvent the wheel.  I remember the days of writing countless classes/algorithms that already exist.  good times.  that also answers part of the the "why are you learning C stuff" question.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.