![]() |
Re: C Programming Homework Help Needed
The function you are looking for is called "sprintf".
Use it just like you would use printf to print a float. The trick is that it throws it into a string, rather than at the console. Code:
char * buffer = new char[10]; |
Re: C Programming Homework Help Needed
Is there a header file you need to include?
I'm getting a 'no prototype for function' error for the sprintf function. |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
The first line: char * buffer = new char[10]; is confusing me. |
Re: C Programming Homework Help Needed
Quote:
char buffer[10]; /* bigger than 10 is a good idea */ If a static or automatic array (depending on the location of the declaration) is good enough, or char *buffer; buffer = malloc(10); /* again, bigger than 10 is a good idea */ to allocate the storage using the heap allocator in C. Eugene |
Re: C Programming Homework Help Needed
1 Attachment(s)
I am using this code:
Code:
#include <stdio.h> |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
Now how can I use the char value? I tried: printf("%c", buffer); But that printed out an an arrow pointing up.:p |
Re: C Programming Homework Help Needed
Quote:
printf("%s", buffer); printf("%c", buffer[0]); would print the first char Eugene |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
Code:
float f = 12.4;If you ever have a simple question about a function, Google is your friend. (Just put in the function name or "C functionName".) Even Wikipedia has an article about most standard functions. |
Re: C Programming Homework Help Needed
1 Attachment(s)
This code is supposed to convert a base 10 number, 123, into binary and display the value. Somewhere in the conversion, the division loop stops before it should, for a reason that I cannot identify.
Output with some diagnostic print statements attached. Code:
/* Patrick McCarthy */ |
Re: C Programming Homework Help Needed
A few notes to make your life easier:
The switch(base) structure is confusing. I suggest replacing it with something that more clearly states what you want. I'd recommend something like Code:
if(base <2 || base > 10) { Error !!}Your printf statement doesn't really tell the user what you are going to do with the number. Put a more informative request in there. "Please enter the base of the number (between 2 and 10)" Please comment what q and r are. They are probably quotient and remainder, but explicit = maintainable. Okay, enough with the picky details. Pain in the patookus, but trust me, you'll thank me later. To clarify divide, I've used pass by reference, rather than by pointer. Also, I've added some more debuggging! Code:
void divide(int D, int N, int &q, int &r)_____________________________________ Thought of a few more helper functions. Code:
int convertFromBaseToBinary(int base, char * number_string)__________________________ Just noticed another bug. for(j = 1; q[j] >= 0; j++) will never terminate. I think you want q[j]>0 or maybe even q[j-1]>0 Edit 3 _________________________ And for more giggles, here is some Python Code:
def convertToBase(num, base): |
Re: C Programming Homework Help Needed
1 Attachment(s)
I'm having a little bit of trouble now. See output.
Code:
/* Patrick McCarthy */ |
Re: C Programming Homework Help Needed
You only half integrated my changes to divide.
q and r are coming in as references. You do not need to declare them again. Also, divide(base, q[j-1], &q[j], &r[j]); should read as divide(base, q[j-1], q[j], r[j]); I'm going to be offline for a while. Good luck! |
| All times are GMT -5. The time now is 13:22. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi