Solved

string

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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 and use conditional statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now