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;
}

>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.

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 */
}

> 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.

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 :)

>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.

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.

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

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?

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?

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

> 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;
}
This question has already been answered. Start a new discussion instead.