944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1833
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 30th, 2007
0

why isnt it working!?!

Expand Post »
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)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimwalther is offline Offline
22 posts
since Sep 2007
Sep 30th, 2007
0

Re: why isnt it working!?!

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 30th, 2007
0

Re: why isnt it working!?!

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 30th, 2007
0

Re: why isnt it working!?!

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 30th, 2007
0

Re: why isnt it working!?!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimwalther is offline Offline
22 posts
since Sep 2007
Sep 30th, 2007
0

Re: why isnt it working!?!

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 30th, 2007
0

Re: why isnt it working!?!

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimwalther is offline Offline
22 posts
since Sep 2007
Sep 30th, 2007
0

Re: why isnt it working!?!

Have you drawn a flow chart or written some sudo code?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 30th, 2007
0

Re: why isnt it working!?!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jimwalther is offline Offline
22 posts
since Sep 2007
Sep 30th, 2007
0

Re: why isnt it working!?!

That's good.

If we address point one.

C++ Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help reading from a file.
Next Thread in C++ Forum Timeline: Knowing if the variable is wchar_t* or a char*.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC