• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Error about variable type

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
      int i, b = 0, snp[8], t = 7, stp = 0, check, n;

      clrscr();
      printf("\nInput a binary number : ");
      scanf("%d", &n);

      for (i = 7; i >= 0 ; i--)
      {
            if (n - pow(10, i) >= 0)
                  snp[b] = 1;
            else
                  snp[b] = 0;
            //n = n - pow(10, i);
            b++;
      }

      for (i = 0; i < 8 ; i++)
      {
            if ((snp[i] != 0) && (snp[i] != 1))
            {
                  printf("This is not a binary number.");
                  getch();
                  exit();
            }
      }

      for (i = 0; i < 8 ; i++)
      {
            //printf("%d ", snp[i]);
            stp += snp[i] * pow(2, t);
            t = t -1;
      }

      printf("The decimal of %d is %d.", n, stp);
      getch();
}

if i change int i, b = 0, snp[8], t = 7, stp = 0, check, n;
to int i, b = 0, check, n;
long int snp[8], t = 7, stp = 0;
it doesn't work
i want input a binary number like 11111111 and then it chane to decimal. How can i do ?
0
Khanh Doan
Asked:
Khanh Doan
3 Solutions
 
e_tadeuCommented:
There is an error in the first for loop. Why did you comment that line n = n - pow(10, i)? It should be in the code, but in another place (inside the if (n - pow(10, i) >= 0)).

Also, don't forget to change your printf to:

printf("The decimal of %d is %ld.", n, stp);

Because your stp is now a long!!
0
 
brettmjohnsonCommented:
This is certainly the hard way.  Why not read the input as a string, then loop over the characters, examining them [verifying they are '0' or '1'], then adding them to the number you are building.

Hints:
num << 1 multiplies num by 2
if char ch = '1', then ch - '0' = 1

This would probably be cheating for your homework assignment, but I would just use strtol() with a radix of 2.
0
 
Khanh DoanSenior DeveloperAuthor Commented:
When i change  int i, b = 0, snp[8], t = 7, stp = 0, check, n;
to int i, b = 0, check, t = 7, stp = 0;
long int snp[8], n;

then I input 11111 and then it show : The decimal of 589738503 is 255.
why ?
0
 
cwwkieCommented:
> long int snp[8], n;

> scanf("%d", &n);

If you change n into a long int, you must change all the %d into %ld in printf/scanf too. Otherwise the result is undefined.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now