943,694 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1121
  • C RSS
Sep 29th, 2007
0

Error message!

Expand Post »
hey there;
i wanted to write a program to convert binary to decimal.
i did that using the " character approach".
it works just fine.
then i decided to enhance the program by adding an error message whenever the input are invalid (i.e anything other than 0, 1 ), but i havent been able to figure it out yet.
i've been playing around with it but it is still not coming.
my knowledge of C program is very limited ( i've just started learning C).
was wondering if you guys could possibly help me with it.
any help is appreciated!

here is the codes of what i wrote:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main ()
  5. {
  6. int decimalValue=0;
  7. int nextDigit;
  8. char binary;
  9.  
  10. printf(" Enter Binary Number ! \n");
  11. binary = getchar();
  12.  
  13. while (binary == '1' || binary == '0')
  14. {
  15. if ( binary == '1')
  16. {
  17. nextDigit=1;
  18. }
  19.  
  20. else
  21. {
  22. nextDigit=0;
  23. }
  24.  
  25.  
  26.  
  27. decimalValue = decimalValue*2 + nextDigit ;
  28. binary = getchar();
  29.  
  30. }
  31.  
  32. printf ("The Decimal Value= %i\n", decimalValue);
  33.  
  34. system("PAUSE");
  35. return 0;
  36. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
megan-smith is offline Offline
13 posts
since Sep 2007
Sep 29th, 2007
0

Re: Error message!

hey there;
i wanted to write a program to convert binary to decimal.
i did that using the " character approach".
it works just fine.
then i decided to enhance the program by adding an error message whenever the input are invalid (i.e anything other than 0, 1 ), but i havent been able to figure it out yet.
i've been playing around with it but it is still not coming.
I must be missing something because there seems to be no attempt in the posted code to deal with invalid input. We therefore can't tell what you are doing wrong.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Sep 29th, 2007
0

Re: Error message!

here's the best i came up with.
but sitll, it doesn't account for invalid numbers which start with 0 or 1 ( ie 123)!
thanks in advance.
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main ()
  5. {
  6. int decimalValue=0;
  7. int nextDigit;
  8. char binary;
  9.  
  10.  
  11. printf("Enter Binary Number ! \n");
  12. binary = getchar();
  13.  
  14.  
  15. if (binary > '1' || binary < '0')
  16. {
  17. printf("ERROR! \n");
  18. }
  19. else
  20. {
  21. while (binary == '1' || binary == '0')
  22. {
  23. if ( binary == '1')
  24. {
  25. nextDigit=1;
  26. }
  27.  
  28. else
  29. {
  30. nextDigit=0;
  31. }
  32.  
  33.  
  34.  
  35. decimalValue = decimalValue*2 + nextDigit ;
  36. binary = getchar();
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. printf ("The Decimal Value = %i\n", decimalValue);
  50.  
  51. }
  52.  
  53.  
  54.  
  55. system("PAUSE");
  56. return 0;
  57. }
Last edited by megan-smith; Sep 29th, 2007 at 1:31 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
megan-smith is offline Offline
13 posts
since Sep 2007
Sep 29th, 2007
0

Re: Error message!

Dave's short version: read all user input as the text it is; interpret it at your leisure.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 29th, 2007
0

Re: Error message!

Dave's short version: read all user input as the text it is; interpret it at your leisure.
but how?
I read the very first input.
but i'm stuck when it comes to the the second input and third and ....
it would have been great had you explained a bit more.
thanks though.
cheers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
megan-smith is offline Offline
13 posts
since Sep 2007
Sep 29th, 2007
0

Re: Error message!

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 29th, 2007
0

Re: Error message!

> but sitll, it doesn't account for invalid numbers which start with 0 or 1 ( ie 123)!
Maybe put the 'if' statement inside the 'while' loop ?

But separating input from conversion is good as well, as Dave has remarked.
Last edited by Salem; Sep 29th, 2007 at 3:24 am.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 29th, 2007
0

Re: Error message!

putting if statement in the while loop didn't seem to work. at least the way i did it .
Could you please show on the codes what you meant.

how do you seperate the input form the conversion?
the link Dave gave out was way too high-level for me, which i did'nt really understand.
Could you be more explicit and clear.
I told ya.
I'm just beginning.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
megan-smith is offline Offline
13 posts
since Sep 2007
Sep 29th, 2007
0

Re: Error message!

So post your latest effort, not a description of what you may have tried.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: Dynamic Memory Allocation
Next Thread in C Forum Timeline: how to transpose a matrix





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


Follow us on Twitter


© 2011 DaniWeb® LLC