Error message!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 13
Reputation: megan-smith is an unknown quantity at this point 
Solved Threads: 0
megan-smith megan-smith is offline Offline
Newbie Poster

Error message!

 
0
  #1
Sep 29th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Error message!

 
0
  #2
Sep 29th, 2007
Originally Posted by megan-smith View 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.
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 13
Reputation: megan-smith is an unknown quantity at this point 
Solved Threads: 0
megan-smith megan-smith is offline Offline
Newbie Poster

Re: Error message!

 
0
  #3
Sep 29th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 253
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error message!

 
0
  #4
Sep 29th, 2007
Dave's short version: read all user input as the text it is; interpret it at your leisure.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 13
Reputation: megan-smith is an unknown quantity at this point 
Solved Threads: 0
megan-smith megan-smith is offline Offline
Newbie Poster

Re: Error message!

 
0
  #5
Sep 29th, 2007
Originally Posted by Dave Sinkula View Post
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 253
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Error message!

 
0
  #6
Sep 29th, 2007
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Error message!

 
0
  #7
Sep 29th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 13
Reputation: megan-smith is an unknown quantity at this point 
Solved Threads: 0
megan-smith megan-smith is offline Offline
Newbie Poster

Re: Error message!

 
0
  #8
Sep 29th, 2007
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.
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Error message!

 
0
  #9
Sep 29th, 2007
So post your latest effort, not a description of what you may have tried.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 994 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC