input problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Al

    #1

    input problem

    I am typing the following code to convert a binary number to decimal.
    It usually works fine, but with some numbers, it reads strange values:

    #include <iostream>
    using namespace std;

    int main()
    {
    int binar;
    cin >> binar;
    // cout <<binar <<endl;
    int dec=0, num=1;
    dec += (binar%10) * 1;
    binar /= 10;
    while (binar>1)
    {
    dec += (binar%10) * num*2;
    num*=2;
    binar /= 10;
    }
    dec += binar * num*2;
    cout << dec <<endl;
    system("pause") ;
    return 0;
    }

    when I debug or I use the comment line with a value: 10011001100, it
    prints (the value of binar is) 2 and the decimal is also 2.


    Same problem with the following code in C, but the values are a lot
    different: 1421066508 for binar value and 2028 for dec:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int bin, dec=0, num=1;
    scanf("%d", &bin);
    printf("%d\n", bin);
    dec += (bin%10) * 1;
    bin /= 10;
    while (bin>1)
    {
    dec += (bin%10) * num*2;
    num*=2;
    bin /= 10;
    }
    dec += bin * num*2;
    printf("%d\n", dec);
    system("pause") ;
    return 0;
    }



    do you know what the problem is??
    is it for overflow of integer?
    help!

  • Jim Langston

    #2
    Re: input problem

    "Al" <alberto.bietti @gmail.com> wrote in message
    news:1132783315 .927888.34980@g 44g2000cwa.goog legroups.com...[color=blue]
    >I am typing the following code to convert a binary number to decimal.
    > It usually works fine, but with some numbers, it reads strange values:
    >
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int binar;
    > cin >> binar;
    > // cout <<binar <<endl;
    > int dec=0, num=1;
    > dec += (binar%10) * 1;
    > binar /= 10;
    > while (binar>1)
    > {
    > dec += (binar%10) * num*2;
    > num*=2;
    > binar /= 10;
    > }
    > dec += binar * num*2;
    > cout << dec <<endl;
    > system("pause") ;
    > return 0;
    > }
    >
    > when I debug or I use the comment line with a value: 10011001100, it
    > prints (the value of binar is) 2 and the decimal is also 2.[/color]

    Max the max range on an int? For signed it's 2147483647 on Microsoft
    systems anyway. 10011001100 is greater than 2147483647. You are
    overflowing your int.

    I would suggest you use std::string or char array instead.
    [color=blue]
    >
    >
    > Same problem with the following code in C, but the values are a lot
    > different: 1421066508 for binar value and 2028 for dec:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int bin, dec=0, num=1;
    > scanf("%d", &bin);
    > printf("%d\n", bin);
    > dec += (bin%10) * 1;
    > bin /= 10;
    > while (bin>1)
    > {
    > dec += (bin%10) * num*2;
    > num*=2;
    > bin /= 10;
    > }
    > dec += bin * num*2;
    > printf("%d\n", dec);
    > system("pause") ;
    > return 0;
    > }
    >
    >
    >
    > do you know what the problem is??
    > is it for overflow of integer?
    > help!
    >[/color]


    Comment

    • Al

      #3
      Re: input problem


      Jim Langston wrote:[color=blue]
      > I would suggest you use std::string or char array instead.[/color]

      how could I use strings for manipulating numbers??
      Should I consider them numbers?or do I need to access to each element
      of the array?
      could you show me a little example?

      Comment

      • Brian - qbg

        #4
        Re: input problem

        One thing you could do is use a bigger datatype, but that would still
        have it limits.

        Comment

        • Karl Heinz Buchegger

          #5
          Re: input problem

          Al wrote:[color=blue]
          >
          > Jim Langston wrote:[color=green]
          > > I would suggest you use std::string or char array instead.[/color]
          >
          > how could I use strings for manipulating numbers??[/color]

          Jim suggests to use the string only for the binary 'number'.

          std::string binar;
          cin >> binar;

          If you need to get the value of an digit, you can simply
          calculate it.
          Say your string contains "1001"
          and you need the numerical value of the digit at index position
          2, you can simply do
          int digit = binar[2] - '0';

          now digit will only be 0 or 1

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • \/Gogineni\/

            #6
            Re: input problem

            You need to declare your binar as unsigned int to avoid sign bit

            Comment

            • \/Gogineni\/

              #7
              Re: input problem

              You need to declare your binar as unsigned int to avoid sign bit

              Comment

              Working...