Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

string

Posted on 1998-07-21
5
Medium Priority
?
263 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

604 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