Solved

string

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

Industry Leaders: 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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

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