[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error about variable type

Posted on 2006-06-04
4
Medium Priority
?
255 Views
Last Modified: 2010-04-15
#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
Comment
Question by:Khanh Doan
4 Comments
 
LVL 4

Assisted Solution

by:e_tadeu
e_tadeu earned 450 total points
ID: 16829919
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
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 300 total points
ID: 16829942
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
 
LVL 10

Author Comment

by:Khanh Doan
ID: 16830089
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
 
LVL 14

Accepted Solution

by:
cwwkie earned 750 total points
ID: 16832275
> 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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question