| | |
Error message!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
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:
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:
C Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> int main () { int decimalValue=0; int nextDigit; char binary; printf(" Enter Binary Number ! \n"); binary = getchar(); while (binary == '1' || binary == '0') { if ( binary == '1') { nextDigit=1; } else { nextDigit=0; } decimalValue = decimalValue*2 + nextDigit ; binary = getchar(); } printf ("The Decimal Value= %i\n", decimalValue); system("PAUSE"); return 0; }
•
•
•
•
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
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.
but sitll, it doesn't account for invalid numbers which start with 0 or 1 ( ie 123)!
thanks in advance.
C Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> int main () { int decimalValue=0; int nextDigit; char binary; printf("Enter Binary Number ! \n"); binary = getchar(); if (binary > '1' || binary < '0') { printf("ERROR! \n"); } else { while (binary == '1' || binary == '0') { if ( binary == '1') { nextDigit=1; } else { nextDigit=0; } decimalValue = decimalValue*2 + nextDigit ; binary = getchar(); } printf ("The Decimal Value = %i\n", decimalValue); } system("PAUSE"); return 0; }
Last edited by megan-smith; Sep 29th, 2007 at 1:31 am.
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Dave's short version: read all user input as the text it is; interpret it at your leisure.
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
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Toshiba IDE1 error message (Windows NT / 2000 / XP)
- MERGED: Hardware/Driver Problems-Error Message (Windows 95 / 98 / Me)
- wupdater.exe error message (Web Browsers)
- Please help with DLL error message! (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: Dynamic Memory Allocation
- Next Thread: how to transpose a matrix
Views: 995 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






