|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Quote:
Idk how you would get each number individually but to change the number into its ASCII code you would do # + '0' to get the ascii code of the number |
|
#2
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Well, I have a float variable that I need to be able to display using WinBGI using the text function that only accepts char strings. So, I really don't know exactly.
|
|
#3
|
|||
|
|||
|
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]; float f = 12.4; sprintf(buffer,"%f",f); |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Re: C Programming Homework Help Needed
I believe you need to include stdio.h.
|
|
#6
|
||||
|
||||
|
Re: C Programming Homework Help Needed
That made that error go away but I still don't really understand how to use the function.
The first line: char * buffer = new char[10]; is confusing me. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
||||
|
||||
|
Re: C Programming Homework Help Needed
I am using this code:
Code:
#include <stdio.h>
int main(void)
{
char *buffer;
buffer = malloc(10);
float f = 12.4;
sprintf(buffer,"%f",f);
return 0;
}
|
|
#9
|
|||
|
|||
|
Re: C Programming Homework Help Needed
malloc() is likely declared in stdlib.h
|
|
#10
|
||||
|
||||
|
Re: C Programming Homework Help Needed
It was. Thanks.
Now how can I use the char value? I tried: printf("%c", buffer); But that printed out an an arrow pointing up. ![]() |
|
#11
|
|||
|
|||
|
Re: C Programming Homework Help Needed
Quote:
printf("%s", buffer); printf("%c", buffer[0]); would print the first char Eugene |
|
#12
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Quote:
![]() |
|
#13
|
|||||
|
|||||
|
Re: C Programming Homework Help Needed
Quote:
Code:
float f = 12.4;
printf("%f",f);
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. |
|
#14
|
||||
|
||||
|
Re: C Programming Homework Help Needed
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 */
#include <stdio.h>
#include <math.h>
void base_convert(int base);
void convert_to_binary(int base, int number);
void divide(int D, int N, int *pq, int *pr);
int main(void)
{
int base;
int base_error;
/* Gets the base from user and validates the value */
do{
base_error = 0;
/* Collects base value from user */
printf("Please enter an integer between 2 and 10 =>");
fflush(stdin);
scanf("%d",&base);
/* Validate entry */
switch (base)
{
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
break;
default:
base_error = 1;
printf("Invalid entry\n");
}
}
while(base_error);
/* Finds base N value of NUMBER */
// base_convert(base);
/* Converts number to Binary */
convert_to_binary(base, 123);
return 0;
}
void base_convert(int base)
{
int i;
int new[3];
int NUMBER[3];
int answer;
//The base 10 number to be converted
NUMBER[0] = 3;
NUMBER[1] = 2;
NUMBER[2] = 1;
for(i = 0; i < 3; i++)
{
new[i] = ( NUMBER[i] * pow(base,i) );
// Diagnostic
// printf("i = %d; NUMBER[i] = %d; new[i] = %d\n", i, NUMBER[i], new[i]);
};
answer = new[0] + new[1] + new[2];
printf("%d%d%d(Base 10) = %d(Base %d)", NUMBER[2],NUMBER[1],NUMBER[0],answer, base);
return;
}
void convert_to_binary(int base, int number)
{
int j,h;
int q[16];
int r[16];
q[0] = number;
for(j = 1; q[j] >= 0; j++)
{
printf("j=%d\n",j);
divide(base, q[j-1], &q[j], &r[j]);
}
for(h = j; h <= 0; h--)
{
printf("%d", r[h]);
}
}
void divide(int D, int N, int *pq, int *pr)
{
int q,r;
q = N / D;
r = N % D;
printf("Quotient: %d\nRemainder: %d\n\n", q,r);
*pq = q;
*pr = r;
}
|
|
#15
|
|||
|
|||
|
help on palindrome
i have make a program that can trace palindrome number but i need it to loop over and over again till i keyin -1 then only the program terminate this is the code that i have used: Code:
#include<stdio.h>
#include<string.h>
void main()
{
char strsrc[5];
char strtmp[5];
printf("\n Enter a five-digit number ( -1 to end ): "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n %s is a palindrome\n",strsrc);
else
printf("\n %s is not a palindrome\n",strsrc);
}
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| programming help needed | krunal | Programming | 3 | 29-01-2007 00:18 |
| Help on a Homework Assignment. | Pavan Dave | General Forum | 1 | 13-09-2006 14:20 |
| HELP-URGENT PROGRAMMING HELP NEEDED | Rohith Surampudi | Lego Mindstorm Discussion | 1 | 24-03-2006 23:05 |
| URgent Programming Help needed | rcubes85 | Programming | 4 | 15-02-2005 23:21 |