954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

why isnt it working!?!

Hey, I have no idea why this code isnt working, im new to C++ so maybe im missing some fundamentals or something. But to me it looks perfectly fine.. the purpose of this program is to change binary into user code... if someone could help me out it would be very appreciated ^.^

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

int main ()
{
    
    
    
    int nextDigit,oneDigit;
    char binaryNumber;
    //prompt user for binary #
    printf("Please enter a binary number\n");
    scanf("%c", &binaryNumber);
    /*assign variable decimalValue 0*/
    int decimalValue=0;
    
    
    /*obtain variable nextDigit
    (the first digit in the binary number, then the next and so on.)*/


    oneDigit=getchar();
    
    if (oneDigit != 1 || oneDigit != 0)
    printf("invalid binary digit was entered.\n");
             else
             {
                     while (oneDigit == '1' || oneDigit == '0');
                           {
                           /*assign variable decimalValue
                           the value of (decimalValue*2)+nextDigit*/
                           decimalValue = ((decimalValue*2) + oneDigit);
                           }
                           printf("%i",decimalValue);
             }
system("PAUSE");
return 0;
}
jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

>why isnt it working!?!

Is your conversion from char to int valid? Is it just a single char you need to read or a string of chars?
Do you want to use old c style syntax to get user input or new c++ style syntax.

Answer these questions before continuing.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
int ch = getchar() ; // reads a char literal '0', '1' etc.
// the integer contains the code point for the literal (depends on the charset)
// to convert it to an integer digit with value 0, 1 etc. 
if( isdigit(ch) )
{
    int digit = ch - '0' ;
    /* use digit */
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

> im new to C++ so maybe im missing some fundamentals or something.
Like the fact that your program is a C program perhaps?

Clarifying which language you're trying to learn would be a good idea, because if you attempt to use some mysterious C/C++ hybrid, it won't be a pretty sight.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I am using Dev-C++... does that help? which parts are C language? im also learning out of he book "Engineering Problem Solving with C:Third Edition by Delores M. Etter.
Thanks for the quick replies :)

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

>I am using Dev-C++
C++ is a multi paradigm language, meaning it can support both c and c++ syntax. Telling us you use Dev-C++ means nothing here.

>which parts are C language
All of it.

If you require some impetus to continue consider re-reading posts 2,3 and 4.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Sorry about that, the compiler shows me no errors and it is a string of zeros and ones
eg. 1010101 would be turned into 85.
As for learning C or C++ would it be possible to see both? Im pretty sure i am learning C if thanks not possible. Vijayan gave a great example but im not able to wrap my head around it enough to implement it into my program >.<. Thanks again.

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Have you drawn a flow chart or written some sudo code?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I myself havent written any but this is what i am trying to do:
Input: A series of binary digits (0's & 1's) followed by any other character.
Output: The decimal value equivalent to the binary number entered.
Pseudo-code:
1. Prompt the user to enter a binary number
2. Assign variable decimalValue 0
3. Get the first binary digit and Assign variable nextDigit the value of the digit
4. While (there are still digits to be processed)
4.1 Assign variable decimalValue the value of (decimalValue * 2) + nextDigit
4.2 Get the next binary digit and Assign nextDigit the value of the digit
5. Output the value of variable decimalValue

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

That's good.

If we address point one.

printf("Please enter a binary number\n");
    scanf("%c", &binaryNumber);

You will see that your code prompts the user for a single character. What you need to do is prompt the user for a string of characters.

How would you do that do you think?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I'm kind of guessing here.. but a float? but that would be saying like:
111 is equal to one hundred and eleven wouldnt it? instead of like, singular numbers?

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

No, try again. The clue is here:- "What you need to do is prompt the user for a string of characters."

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

so no actual change in programing, just the statment?

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Yes you need to change the programming syntax too.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I dont know how to do that >.< haha. time to learn i guess, lesson 101 :P

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

He he, Good idea.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

> I dont know how to do that ...
i suppose you have not learned about arrays yet. in which case, use your original idea

/*obtain variable nextDigit
(the first digit in the binary number, then the next and so on.)*/
oneDigit=getchar();


of reading one digit at a time. eg.

#include <stdio.h>
#include <limits.h>

int main()
{
  const int MAXBITS = sizeof(int) * CHAR_BIT ;
  unsigned int value = 0 ;
  int n = 0 ;
  printf("Please enter a binary number\n");
  
  for( ; n<MAXBITS ; ++n )
  {
    int ch = getchar() ;
    if( (ch=='0') || (ch=='1') )
    {
      int bit = ch - '0' ;
      value = value*2 + bit ;
    }
    else break ;
  }
  
  printf( "\nvalue: %u\n", value ) ;
  return 0;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Thanks so much guys, got it to work ^.^

jimwalther
Newbie Poster
22 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You