Solved

string

Posted on 1998-07-21
5
238 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

786 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