Solved

string

Posted on 1998-07-21
5
253 Views
Last Modified: 2010-04-15
I´m reading data whit bioscom. whit this I can read in the screen a character (putch()...) but I need convert this character to float. (I read in the screen i.e.: "01.24")
I tried whit atof(), but I think that I have a mistake because I used strlen and read 34 (the string is [5] characters).
I´m working whit TurboC++ ver. 2.0. (in 386).
I need help you about this!!!
Thanks!!
0
Comment
Question by:juancarlos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
duneram earned 100 total points
ID: 1251765
Hi,
Two ways to solve it.

1.  Just use the scanf function.  It can read the character as a float:

float x;
int num_fields_read;
printf("Enter the float value ");
num_fields_read = scanf("%f", &x); /* its actually gettting input now */
printf(" Just read %f\n", x);


or 2) continue:  Go ahead and read this in as character, and then use something like this:

before you use your array set the whole thing to zero.  This wil help prevent buffer overflows   something like this:

char data[10];
float result;
memset(data,0,sizeof(data));
/* read your data in from the screen */
result = atof(data);
printf("Original data '%s', is %f (float)\n", data, result);

0
 
LVL 1

Expert Comment

by:newexpert
ID: 1251766
duneram:

genius, sizeof(data) == 10???  think again!!
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1251767
Sorry duneram
data is array not pointer you were right.
0
 
LVL 2

Expert Comment

by:duneram
ID: 1251768
Thats okay we all have our days....  

He could also use the char data[10]={{0}};  

On some compilers you need two open close braces to get rid of the warnings, others 1 suffices.
0
 

Author Comment

by:juancarlos
ID: 1251769
O.K Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question