| | |
why isnt it working!?!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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 ^.^
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon; Sep 30th, 2007 at 10:01 am. Reason: add code tags
>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.
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.
Last edited by iamthwee; Sep 30th, 2007 at 9:21 am.
*Voted best profile in the world*
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
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 */ }
>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.
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.
Last edited by iamthwee; Sep 30th, 2007 at 3:53 pm.
*Voted best profile in the world*
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.
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
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.
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?
If we address point one.
C++ Syntax (Toggle Plain Text)
printf("Please enter a binary number\n"); scanf("%c", &binaryNumber);
How would you do that do you think?
*Voted best profile in the world*
![]() |
Similar Threads
- ASP site not working when uploaded (ASP.NET)
- my html code isnt working please help (JavaScript / DHTML / AJAX)
- msn messenger 7.0 isnt working (Windows 95 / 98 / Me)
- Sound isnt working! Help! (Motherboards, CPUs and RAM)
- Fan isnt Running!!?!! (Cases, Fans and Power Supplies)
- Sound Card isnt working! (PCI and Add-In Cards)
- HELP -my Date code isnt working (ASP)
- CD-ROm isnt working correctly (Storage)
Other Threads in the C++ Forum
- Previous Thread: Need help reading from a file.
- Next Thread: Knowing if the variable is wchar_t* or a char*.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






