>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
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>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
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
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
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
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
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