why isnt it working!?!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 22
Reputation: jimwalther is an unknown quantity at this point 
Solved Threads: 0
jimwalther's Avatar
jimwalther jimwalther is offline Offline
Newbie Poster

why isnt it working!?!

 
0
  #1
Sep 30th, 2007
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 ^.^
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main ()
  5. {
  6.  
  7.  
  8.  
  9. int nextDigit,oneDigit;
  10. char binaryNumber;
  11. //prompt user for binary #
  12. printf("Please enter a binary number\n");
  13. scanf("%c", &binaryNumber);
  14. /*assign variable decimalValue 0*/
  15. int decimalValue=0;
  16.  
  17.  
  18. /*obtain variable nextDigit
  19.   (the first digit in the binary number, then the next and so on.)*/
  20.  
  21.  
  22. oneDigit=getchar();
  23.  
  24. if (oneDigit != 1 || oneDigit != 0)
  25. printf("invalid binary digit was entered.\n");
  26. else
  27. {
  28. while (oneDigit == '1' || oneDigit == '0');
  29. {
  30. /*assign variable decimalValue
  31.   the value of (decimalValue*2)+nextDigit*/
  32. decimalValue = ((decimalValue*2) + oneDigit);
  33. }
  34. printf("%i",decimalValue);
  35. }
  36. system("PAUSE");
  37. return 0;
  38. }
Last edited by Ancient Dragon; Sep 30th, 2007 at 10:01 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: why isnt it working!?!

 
0
  #2
Sep 30th, 2007
>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.
Last edited by iamthwee; Sep 30th, 2007 at 9:21 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: why isnt it working!?!

 
0
  #3
Sep 30th, 2007
  1. int ch = getchar() ; // reads a char literal '0', '1' etc.
  2. // the integer contains the code point for the literal (depends on the charset)
  3. // to convert it to an integer digit with value 0, 1 etc.
  4. if( isdigit(ch) )
  5. {
  6. int digit = ch - '0' ;
  7. /* use digit */
  8. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: why isnt it working!?!

 
0
  #4
Sep 30th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: jimwalther is an unknown quantity at this point 
Solved Threads: 0
jimwalther's Avatar
jimwalther jimwalther is offline Offline
Newbie Poster

Re: why isnt it working!?!

 
0
  #5
Sep 30th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: why isnt it working!?!

 
0
  #6
Sep 30th, 2007
>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.
Last edited by iamthwee; Sep 30th, 2007 at 3:53 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: jimwalther is an unknown quantity at this point 
Solved Threads: 0
jimwalther's Avatar
jimwalther jimwalther is offline Offline
Newbie Poster

Re: why isnt it working!?!

 
0
  #7
Sep 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: why isnt it working!?!

 
0
  #8
Sep 30th, 2007
Have you drawn a flow chart or written some sudo code?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 22
Reputation: jimwalther is an unknown quantity at this point 
Solved Threads: 0
jimwalther's Avatar
jimwalther jimwalther is offline Offline
Newbie Poster

Re: why isnt it working!?!

 
0
  #9
Sep 30th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: why isnt it working!?!

 
0
  #10
Sep 30th, 2007
That's good.

If we address point one.

  1. printf("Please enter a binary number\n");
  2. 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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC