Hector Sanabria
Hector Sanabria

Reputation: 3

Error when using array elements in conditional statements in C

I have done my fair share of studying the C language and came across this inconsistency for which I cannot account. I have searched everywhere and reviewed all data type definition and relational syntax, but it is beyond me.

From the book C How to Program, there is a question to make a binary to decimal converter where the input must be 5-digits. I developed the follow code to take in a number and, through division and remainder operations, split it into individual digits and assign each to an element in of an array. The trouble arises when I try to verify that the number entered was indeed binary by checking each array element to see whether it is a 1 or 0.

Here is the code:

#include <stdio.h>

int power (int x, int y); //prototype

int main(void)
{
    int temp, bin[5], test;
    int n=4, num=0;
//get input
    printf("%s","Enter a 5-digit binary number: ");
    scanf("%d", &temp);

//initialize array
    while(n>=0){
        bin[n]=temp/power(10,n);
        temp %= power(10,n);
        n--;    }

//verify binary input
for (test=4; test>=0; test--){
    if ((bin[n]!=0)&&(bin[n]!=1)){
        printf("Error. Number entered is not binary.\n");
        return 0;   }

//convert to decimal 
    while(n<=4){
        num+=bin[n]*power(2,n);
        n++;    }

    printf("\n%s%d\n","The decimal equivalent of the number you entered is ",num);
    return 0;
}

//function definition
int power(int x, int y)
{
    int n, temp=x;
    if(y==0) return 1;
    for(n=1; n<y; n++){
        temp*=x;    }
    return temp;
}

Could someone explain to me why regardless of input (whether: 00000, or 12345), I always get the error message? Everything else seems to work fine. Thank you for your help.

Update: If the if statement is moved to the while loop before. This should still work right?

Update2: Never mind, I noticed my mistake. Moving the if statement to the while repetition before does work given the solution supplied by sps and Kunal Tyagi.

Upvotes: 0

Views: 798

Answers (3)

sps
sps

Reputation: 2720

One issue is that, while checking if the number is binary or not, you are returning at wrong place. You need to return only if the number is not binary. But you are returning outside the if condition. So your program returns no matter what the input is.

for (test=4; test>=0; test--){
    if ((bin[test]!=0)&&(bin[test]!=1))
        printf("Error, numbered entered was not binary.\n");
        // Issue here, you are returning outside if
        return 0; }  //exit program 

You can change that to:

for (test=4; test>=0; test--){
    if ((bin[test]!=0)&&(bin[test]!=1)) {
         printf("Error, numbered entered was not binary.\n");
         // Return inside the if
         return 0; // exit program
    }                
} 

There is one more issue. Before you convert your number to decimal, you need to set n = 0;

//convert to decimal
n = 0; /* need to set n to zero, because by now  
          n will be -1.
          And initially when n is -1, accessing 
          bin[-1] will result in undefined behavior
       */ 
while(n<=4){
    num+=bin[n]*power(2,n);
    n++;    }

Upvotes: 0

Kunal Tyagi
Kunal Tyagi

Reputation: 577

This looks like a homework, but your issue is in the brackets. More specifically line 23. That line is not part of the logical if statement despite the indentation (since that doesn't matter in C). No matter what, the program will exit on test=4 after checking the condition.

Solution:

if ((bin[test]!=0)&&(bin[test]!=1)) { // << this brace
        printf("Error, number entered was not binary.\n");
        return 0;   } }  //exit program // notice 2 braces here

Upvotes: 0

dubafek
dubafek

Reputation: 1113

After this

    while(n>=0){
    bin[n]=temp/power(10,n);
    temp %= power(10,n);
    n--;    }

n is set as -1 so when you try to convert to decimal the statement bin[n] is actually bin[-1] so it returns you error.

Upvotes: 1

Related Questions