SEARCH YOUR SOLUTION HERE  

Trouble with program converting decimal to binary

Hi !

I've just started programming, and I've been trying to write a program that converts a decimal number input by the user to a binary number.

Posted On: Thursday 18th of October 2012 01:32:07 AM Total Views:  261
View Complete with Replies




Related Messages:

while(true) loop trouble   (172 Views)
I'm trying to create a simple calculator that keeps prompting the user for input until the user enters the standard input ("number" "operator") and the operator = 'E'. I know in java I'd just use a while(true) loop (as I did in here), but I get the error "true undeclared." I'm new to C and am not sure what I'm doing wrong .. thanks for any help! Here is my code: Code: #include #include main() { double num; double accum; char op; printf("Initialize your Accumulator with data of the form 'number' 'S' which sets the Accumulator \ to the value of your number\n"); scanf("%f %c", &num, &op); while (1) { if (op == 'S') { accum = num; } else if (op == '+') { accum = accum+num; } else if (op == '*') { accum = accum*num; } else if (op == '/') { accum = accum/num; } else if (op == '-') { accum = accum-num; } else if ((num == 0) && (op == '/')) { printf("Can not divide by 0.\n"); } else if (op == '-') { accum = accum-num; } else if ((num == 0) && (op == '/')) { printf("Can not divide by 0.\n"); } else if (op == 'E') { printf("Value in the Accumulator = %d \n", accum); printf("End of Calculations.\n"); exit(EXIT_SUCCESS); /*exits program if user enters E*/ } else { printf("Unknown operator.\n"); } printf("Value in the Accumulator = %f \n", accum); scanf("%f %c", &num, &op); } , Originally Posted by iMalc In one of your scanf's you are correctly using %d and in the rest you are incorrectly usinf %f. That's probably the main issue. Note that you are using different data types for accum and num where it would probably make sense for them to be the same. Note that the same line, plus the printf appear in about 7 places in that code. The best thing to do there would be to remove the duplication. There should only be one scanf line in the whole program. Once you sort out where that one line should go and remove the duplication, you'll have a simpler program and you'll be much happier with the clearer code.
Novice Programmer, having trouble. Netbeans/Cygwin   (239 Views)
I'm new around here. Novice programmer, done a lot with Mathematica and a little with Java. Recently been trying to teach myself C. Anyway, the issue is basically summed up in the picture. I don't know what is missing. Well, I know what is missing, but I don't know how to fix it. I have Netbeans and I installed Cygwin's C/C++ compiler. In Cygwin, there are an absurd number of packages for you to chose from, and I think I got all the ones I needed to program in C. I've done a few basic things (e.g. ", world!") that worked fine. But I get this error saying that something is missing., Originally Posted by laserlight is a non-standard header file and you may not need it. I was trying to create a list and set all of its elements to 0, and a website directed me to this. Should I just do it manually, Originally Posted by oninchowder I'm new around here. Novice programmer, done a lot with Mathematica and a little with Java. Recently been trying to teach myself C. ... I have Netbeans and I installed Cygwin's C/C++ compiler. cygwin is a very non-standard environment. It's for advanced programmers who want a Linux like c++ environment on Windows. I'm going to suggest it's not your best learning tool if you are interested in actually learning C. I would recommend Pelles C instead. It's a C-99 standards driven compiler with a much simpler IDE and the best documentation (in the help file) I've seen. And it's free. Trust me on this, as a novice programmer you don't want to be struggling with a quirky, non-standard environment., Originally Posted by laserlight I second CommonTater's assessment that "cygwin is a very non-standard environment". On the other hand, you managed to get the C compiler setup to compile C programs using Netbeans. Especially if you are comfortable with using Netbeans, I would say stick with the setup that you have now. Pelles C would have been good to avoid the hurdle of the setup, but now that you are past that, the C compiler available from Cygwin is reasonably standard compliant and suitable for learning C. Ok... lets see if we can't get past this... One of the big reasons I recommend Pelles C (for beginner and otherwise) is not "easy setup"... although setup is pretty easy. The main reasons are 1) C-99 standard compliance, 2) Lots of extra C libraries, 3) No C++ muppingsnarf to get in the way, 4) Unquestionably the best documentation of any compiler out there, and 5) a full set of resource tools including icon and image editors for GUI development. #4 wants special mention... How many compilers can you find where you have 100% library documentation supplied as an integral part of the IDE Think about how many times I enjoin people to "look it up in your C Library Documentation"... think how often MinGW, Cygwin, LCC etc. users end up with a blank stare on their faces: "What C Library Documentation". They ignore the suggestion because they don't have the first Idea what I'm talking about. In Pelles C it's as easy as pressing F1 ... it's right there. This is both a powerful learning aid and a helpful tool in every project. There are even help files available from their forums that will integrate Windows API help into the IDE so documentation on windows calls can be that easily available... As you well know, Lase, I've tried other compilers and even other languages... I always end up back on Pelles C... and always for the reasons I've just given. For example: My attempts to learn C++ were flat out destroyed by one simple thing: A near total lack of *accurate* and *compiler specific* documentation. Similarly the whole D escapade ended when I discovered the libraries were open sourced, barely documented and dripping full of crap nobody will ever use, and nobody had successfully created windows libraries for it... Always there is a reason behind where I keep ending up... And don't mistake Pelles C for some beginner's learning aide. It's a plenty serious optimizing compiler that procuces both 32 and 64 bit native executables for windows console and windows GUI. Trust me on this... If I found a better compiler, I'd be recommending it... but I haven't.
Character string, trouble moving pointers.   (228 Views)
, this is my first post so take it easy on me Well i'm trying to select the first bit[0] the string and check if its empty or not and if it isnt i want to be able to remove it and then move the pointer(which is set to 0) to [1] Here is what i have so far: Code: void pop(pointme) { pointme new: if(char[0] ==NULL) { printf("The queue is empty"); } else
trouble counting characters.   (185 Views)
I am having trouble writing a program that counts the amount of th's in an inputted text. I think I have the right code, but I keep getting an error saying that left operand must be an l-value. This would be in the else-if block (thisChar = 'h'). here is my code. Code: int numThs = 0; char thisChar, lastChar = ' '; while (scanf("%c", &thisChar) != EOF){ if (scanf("%c", &thisChar) == 1){ if (thisChar = 't') lastChar = thisChar; else if (thisChar = 'h' && lastChar = 't') numThs++; } } printf("There were %d th's.\n", numThs);
trouble in getting this crc coding right   (241 Views)
hi i am stuck with doing crc coding for c programming i ask for user to define the divisor and dataword and got the augemented dataword but have trouble in divinding the augemented dataword with the divisor and getting the remainder result.i am stuck with the algo there here's my coding Code: #include #include #include int main() { int a[20],b[20],c[20],i,k,j,n,s=0,t=0; printf ( "Welcome to the CRC Encoder/Decoder!\n"); printf ( "press n to exit at anytimes\n"); /* do{*/ printf("enter size of dataword\n"); scanf("%d",&n); printf("enter dataword to be transfer bit by bit\n"); for (i=0;i
Having trouble with Easter Sunday program   (188 Views)
, I am brand new to programming and am having a lot of trouble in the class I had to take this semester. I'm a Biology major, so obviously have no idea when it comes to programming (but this class is a requirement, for some reason). Anyway, I have barely been keeping up with the concepts, and am still getting very confused. Some programs I have written up just fine (like a calculator), but this one I am having a lot of trouble with. Here are the exact words from the HW: Have the user enter a year between 1982 and 2048. You can compute the date for any Easter Sunday using the following formulas (all values are integers): a is a year % 19b is year % 4c is year % 7d is (19 * a + 24) % 30e is (2 * b + 4 * c + 6 * d + 5) % 7 Easter Sunday is March (22 + d + e) Write a program that inputs the year and outputs the date (month and day) of Easter Sunday for that year. Print the input as part of the output. For example: Enter the year (for example, 1999): 1985 Easter is Sunday, the 7, in 1985 Here is what I have so far: Code: #include int main() { printf("name here" \n"); int year; int a ; int b ; int c ; int d ; int e ; int easter_sunday ; a = year % 19 ; b = year % 4 ; c = year % 7 ; d = (19 * a + 24) %30 ; e = (2 * b + 4 * c + 6 * d + 5) % 7 ; easter_sunday = (22 + d + e) ; /* Get year from user*/ printf ("Enter year between 1982 and 2048: ") ; scanf ("%d", year) ; printf ("\n Easter Sunday is March %d", easter_sunday) ; return 0; } Now, I realize it isn't complete (I'm not sure how to differentiate between March and April, and not sure how to get the output into the input), but from what I am able to understand, it should at least be working and giving me 'March - day'. But, I keep getting two warnings. These warnings are '25: format ^ayear^a expects type ^aint *^a, but argument 2 has type ^aint^a', and '16: ^ayear^a is used uninitialized in this function'. I'm sorry if any of this doesn't make any sense. As I said, I am having a lot of trouble understanding this as it's so unlike anything I've ever done before. I think I'm sort of on the right track, but something isn't quite working right.
trouble understanding file option and command line arguments   (323 Views)
I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves through program code. argc and argv look at one line at a time and they only look at what is on the command line. I am not sure how a program is broken up into files for WC to read. this is the code I have so far: Code: /* wc simulate */ #include #include char *pgmname; /* name of this program */ int line_count = 0; int word_count = 0; int byte_count = 0; FILE *fp; void main(argc, argv) int argc; char *argv[]; { int i; char *cp; pgmname = argv[0]; fp = stdin; for(i = 1; i < argc; i++) { cp = argv[i]; if(*cp == '-'){ if(*++cp == '\n'){ line_count++;} } else { if(fp != stdin) { fprintf(stderr, "%s: too many arguments\n", pgmname); exit(1); } fp = fopen(cp, "r") if(fp == NULL) { fprintf(stderr, "%s: unable to read %s\n", pgmname, cp); exit(1); } } printf("%d\n", line_count); } } Right now I have code to just count the lines. I am testing the program on a hello world program below: Code: #include main() { printf(" World!\n"); } when I test the program, I get 0 for line count.
trouble with simple arrays   (258 Views)
I have this assignment that I thought would be super easy, but for some reason I can't get it to work, can you give me some insight Code: #include int main () { char fName[12]; char lName[12]; float payRate[12]; float hours[12]; float salary[12]; int empNbr; int i, n; int a = 0; int b = 0; printf("Enter number of Employees"); scanf("%d", &empNbr); for (i= 0; i < empNbr + 1; i++) {printf("Enter Employee's First Name:"); scanf("%c\n", &fName[a]); printf("Enter Employee's Last Name:"); scanf("%c\n", &lName[a]); printf("Enter pay rate:\n"); scanf("%f\n", &payRate[a]); printf("Enter hours worked:\n"); scanf("%f\n", &hours[a]); /* salary[a] = payRate[a] * hours[a]; a++; */ }//for1 for (n = 0; n < empNbr + 1; n++) { printf("\t First Name \t Last Name \t Pay Rate \t Hours \n"); printf("\t fName[b] \t lName[b] \t payRate[b] \t hours[b] \n"); b++; }//for2 return 0; }//main , it asks for the number of employees and I can enter it. it then asks for the first name, I enter it it asks for the last name and when I enter it, it prints "what is payrate, what is hours, what is first name: all without allowing me to enter pay rate or hours.
trouble with std::find_if()   (171 Views)
this is cut from a larger program. #include #include typedef unsigned int SapId; class Sap { public: explicit Sap(SapId id): id_(id) {} SapId ident () const { return id_; } private: SapId id_; }; typedef std::vector SapList; SapList sap_list_; class SapIdMatchPredicate { public: SapIdMatchPredicate(SapId id): id_(id) {} bool operator() (const Sap& sap) const { return sap.ident() == id_; } private: const SapId id_; }; SapList::iterator find (const SapId sap_id) { SapIdMatchPredicate sap_id_match (sap_id); return std::find_if (sap_list_.begin(), sap_list_.end(), sap_id_match); } int main (void) { SapList::iterator i; i = find (1234); return 0; } I've presumably done something wrong with the functor. I get compilation errors '()' : cannot convert parameter 1 from 'class Sap *' to 'const class Sap &' term does not evaluate to a function the erroneous line is deep in algorithm.h -- Nick Keighley The rabbit snarled and hefted his submachine gun angrily. Ears back and teeth visible, he hissed at the cyborg. Charles Stross "Singularity Sky"
template class specialization trouble   (188 Views)
This seems to work fine: template class D { }; template class D { }; but this: template class D { }; template class D class D { }; produces: test4.cc:17: error: invalid parameter type 'void' test4.cc:12: error: in declaration 'template struct Whatever' test4.cc:17: error: '' is not a valid type for a template constant parameter This: class C is no better. Is there some way to specialize on 'void' here -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
stl::list iterator troubles   (296 Views)
I have two nested for loops which look like this with my selfmade list implementation: for (sShip *s = Ships; s; s = s->next) for (sShip *s2 = s->next; s2; s2 = s2->next) {...} Now how can I do this with stl lists and the iterators The most difficult thing is the start of the inner loop. My outer loop would look like this: for (list::iterator iShip = Ships.begin(); iShip != Ships.end(); ++iShip) But of course I can't begin the second loop with iShip->next..... Any hints
Comparing text - Having a little trouble   (198 Views)
I have a variable of type const * char, and I want to see if the value of that variable is "NONE", how would I go about doing this I've tried AnsiCompareText and SameText, whever I try to compile my program I get errors, I know that I have to include something to get this to work but I don't know what it is. I tried #include and #include , and a few others like that. What do I have to include to get one of these to work, since they both do essentially the same thing, which should I use Or should I use someting else. I was thinking it might be easier to just cast the const * char into a String and do thecharvar == string ("NONE"). What is the fastest/best way - Dan
Custom allocator trouble   (159 Views)
I am trying to write a simple single type custom allocator (and by "allocator" I don't necessarily mean it in the standard library way although as you will see the API is quite similar). I need all this in order to use it over a given chunk of memory (for who wants to know it's memory that comes from POSIX mmap(), shared memory segment) so I cannot use normal new/delete. Because my allocation needs are very basic I do not think I should deal with the complexity of writting a general allocator (ie an allocator that may allocate chunks of various sized bytes alligned for various types) but a simple one that only needs to allocate space for a single value of a known type throughout the allocator lifetime. Thus the custom allocator becomes something as: template class Simpleocator { public: Simpleocator(void* data, size_t len); T* allocate(); void deallocate(T*); }; Because the allocator needs to be able to deal with "holes" of memory it doesn't simply return the address of the next free T object inside an imagined T array (within "data" which is presumed to be given properly alligned for any type) but it needs to keep some kind of freelist to have O(1) allocation/deallocation. To do the freelist it has a nested Block struct such as: struct Block { T info; intrusive_node freelist; }; I deal with "data" as being a Block array (and because "data" originally is properly alligned for any type it means that static_cast(data) + always returns a properly alligned Block address and the "info" field inside it should also be properly alligned for T so I solved the allignment issues this way in a standard compliant manner). The "freelist" member of Block is used to link that Block inside the freelist (using an intrusive container method). The problem I am yet to solve for the moment is to find out the address of Block given the address of "info" from within it in a standard compliant way. "allocate()" will return to the user the address of "info" within Block and the user will give that back to "deallocate()" and the problem is how can I in a standard compliant way get to the address of Block having the address of "info" of that Block A naive solution would have one just reinterpret_cast to Block* the T pointer to info assuming that somehow the first data member of a struct should always have the address equal to the address of the struct and such casting is safe but I do not think the standard allows it, or am I wrong I also thought of another, stranger solution, instead of having Block aggregate T, have it derive it such as: struct Block: T { intrusive_node freelist; }; This would allow a standard compliant way to get to Block because we know that a Base* can be safely converted to Derived* if it really is a Derived instance. But then again if T is a non class type I cannot derive from it. Please help -- Dizzy [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
inheritance with templates trouble   (303 Views)
Sorry if this is an obvious question, but I try to let a template class inherit another template class. The code here under is a simplification of how I try to do that, but the compiler complains that 'c' in line '30 ' wasn't declared. " (g++) test.h: In member function T Test2::give() : test.h:30: error: c was not declared in this scope " I have absolute no idea what the problem and/or solution is, can anybody help me with that please Thank you, Driehoek //////////////////////////////////////// 4: template 5: class Test 6: { 7: public: 8: virtual void 9: set (T i) 10: { 11: c = i; 12: 13: } 14: virtual T 15: give () 16: { 17: return c; 18: } 19: protected: 20: T c; 21: }; 22: 23: template 24: class Test2 : public Test 25: { 26: public: 27: T 28: give () 29: { 30: return c * 2; 31: } 32: }; //////////////////////////////////////// , On Fri, 08 Aug 2008 12:55:21 +0000, richard wrote: Finally I brute forced the solution , I had to change line 30 to: //// return Test::c * 2; //// But I don't really see the logic in this, why do you need to specify the base class
troublesome deque   (152 Views)
hi, I do not know if this worth a cent. we know vector is bad and we should use deque. However, this guy gives me some trouble, (pls correct me if I am wrong). In vector, the mem is guaranteed to be a continous block. however, for deque, NO!! So when we try to get an array out of a vector, we just set the pointer to be the &vec[0], done. For deque. get the address of the first elemen does not guarantee the following mem blocks belongs to the queue.
trouble with structs   (152 Views)
I need some simple help. First, I should apologize: I am a total programming fool in C++ and am trying to get some basic structs working before I attempt classes. I can't seem to get the references to those structs working properly and need help. Suppose I have: struct info { int color; int count; }; struct queue{ int front; info list[15]; }; struct queue holding[20]; First, did I get the syntax right above. Second, Is this the right syntax for setting to 5 the color of list element 0 of holding element 0 Both of these pass through the compiler fine. holding[0].list[0].color = 5 If so, holding[0].list[0].color doesn't seem to appear properly on my VC++ variables list when I run in debug mode. What's wrong.
Template trouble   (147 Views)
What's wrong here template string Class_ini::ConvertToString(T) { ostringstream os; os
trouble with structs   (166 Views)
I need some simple help. First, I should apologize: I am a total programming fool in C++ and am trying to get some basic structs working before I attempt classes. I can't seem to get the references to those ...
I'm having trouble understanding overloaded operators   (609 Views)
, I am trying to understand overloaded operators but am getting confused as to how I can provide a conversion from my number class and the scalar types If I create a class that represents a fictional number type, I ...
ACE+TAO troubles   (126 Views)
I'm having a lot of difficulties getting ACE+TAO to work in BCB 6, despite its supposed native BCB support. In a nutshell, I am trying to use a third-party SDK (Avaya Contact API). The SDK includes ACE+TAO_1.4.6.zip in it. I ...