Hi,
I need to take some input from console window like 1234 but should be read as decimal value and do whatever you want and store the same in character array. can anyone tell me how can i do this? max width of numerical value is 4 bytes.
Note: I should read from console as decimal value.
Loading
VulpesPosted Nov 11, 2011, 9:51 AM
char s[10];
char t[10];
printf("\nInput a string, maximum nine characters : ");
scanf("%9s", s); // ignores any characters after the ninth
printf("\nYou entered %s\n", s);
int i = 0;
char *c = s;
while(*c) t[i++] = *c++;
t[i] = '\0';
printf("\nYou entered %s\n", t);
Sam HobbsPosted Nov 11, 2011, 9:42 PM
I could explain how to do that but later you say "gets data from application" so I am confused. I suspect that you are not asking for what you actually need. You later say "the application sends me 12345" .... "make the string as 123.45". It is very normal for some computer applications to read floating-point data without a decimal point; the decimal point is at a specified location within the number. So it is entirely possible to read 12345, convert that to a float, then divide that by a specific value. The following are two ways of doing that, but note that they both yield the same result. Floating-point arithmetic is imprecise and therefore division by 100 does not yeild the exact correct answer. I will leave it for you to figure out how to be exact; there is likely a way and I hope you can find it if you need to.
float f = atof(s);
f /= 100;
printf("%f\n", f);
char t[10];
float f, w;
int n = strlen(s);
if (n > 9) {
puts("error");
return;
}
strncpy(t, s, n-2);
t[n-2] = 0;
f = atof(t);
strncpy(t, s+n-2, 2);
t[2] = 0;
w = atof(t);
f += w / 100;
printf("%f\n", f);
As for the later requirements, if you can explain why you must copy a string from one to another, one character at a time, then there is probably a better way to do whatever you need to do. Without a reason not to, the best answer is to use strcpy or strncpy to copy a string. Why can't you do that?
Karthik AgarwalPosted Nov 11, 2011, 9:36 AM
void *vp = (void *)s; this line vp can be directly char pointer. i have restriction only on reading from console window, once it's read it's all your wish. minimize it as such as you can?
VulpesPosted Nov 11, 2011, 9:30 AM
char s[10];
I had to create a temporary buffer for vp to point to.
I also created another buffer 't' to copy the chars into to demonstrate the operation was working.
If we were reading the string directly into the first char buffer, then we wouldn't need to copy anything and so wouldn't need the second buffer.
Karthik AgarwalPosted Nov 11, 2011, 9:20 AM
void *vp = (void *)s;
Looks like you just created it to get some address for the other pointer?
Can you tell me a better way, only bcoz of this cause i just can't define one more array or variable?
VulpesPosted Nov 11, 2011, 9:02 AM
Also, you can't dereference a void *. You have to cast it to a non-generic type such as char * before you can do so.
Anyway, FWIW, here's the code:
char s[10];
char t[10];
printf("\nInput a string, maximum nine characters : ");
void *vp = (void *)s;
scanf("%9s", vp); // ignores any characters after the ninth
printf("\nYou entered %s\n", vp);
char *c = (char *)vp;
int i = 0;
while(*c) t[i++] = *c++;
t[i] = '\0';
printf("\nYou entered %s\n", t);
Karthik AgarwalPosted Nov 11, 2011, 8:09 AM
VulpesPosted Nov 11, 2011, 8:05 AM
You can do this directly and then convert to a void * if you need that for some other purpose:
printf("\nYou entered %s", s);
If you entered 123.456, the string would contain 123.45 i.e. a maximum of 6 characters
Karthik AgarwalPosted Nov 11, 2011, 7:31 AM
And one more thing i wanted is how to read a string from console window into a void pointer and copy character by character into the array? Reply ASAP.
if i enter karthik in console window then i should copy each character one after the other into the character array like 'k' first then 'a' then 'r' then 't' then and so on.
VulpesPosted Nov 11, 2011, 7:23 AM
Is the most important thing here to store the numeric value in the char array exactly as it's entered at the console?
The reason I ask is that you could do it a few lines with this:
However, the trouble is that, if you enter 123.45, then the final line will print out 123.44999 because it's the internal representation of the float (which isn't usually exact) that gets converted to a string.
To avoid this you could read it as a string and then check that it is in fact a valid float. If that's what you need, then I'll write the code when I have more time.
Karthik AgarwalPosted Nov 11, 2011, 6:06 AM
VulpesPosted Nov 11, 2011, 5:48 AM
Also could the number include a decimal point?