chpicker
Programmer
- Apr 10, 2001
- 1,316
I know, I'm too used to high level programming languages where I can just use the VAL() function to convert strings to numbers.
I'm writing a program that needs to take an ASCII input file containing numbers and use the numbers to make calculations. I've already figured out how to read the file and write a new one. However, I can't figure out how to convert a string to a float.
Can someone fill this in please? I just can't figure it out. I tried searching through MSDN for all of the functions I could find on the ANSI C string class, the basic_istream class, etc, and I couldn't find anything that looked like it would work.
The program needs to be cross-platform, so I have to stick to ANSI C++. No MFC or other Microsoft inventions.
Ian
I'm writing a program that needs to take an ASCII input file containing numbers and use the numbers to make calculations. I've already figured out how to read the file and write a new one. However, I can't figure out how to convert a string to a float.
Can someone fill this in please? I just can't figure it out. I tried searching through MSDN for all of the functions I could find on the ANSI C string class, the basic_istream class, etc, and I couldn't find anything that looked like it would work.
The program needs to be cross-platform, so I have to stick to ANSI C++. No MFC or other Microsoft inventions.
Code:
[COLOR=blue]#include[/color] <iostream.h>
[COLOR=blue]float[/color] VAL([COLOR=blue]char[/color]* InString)
{
[COLOR=green]// How do I do this?[/color]
}
[COLOR=blue]int[/color] main
{
[COLOR=blue]char[/color] MyString[]="1.45";
[COLOR=blue]float[/color] MyNum=VAL(MyString);
cout << "The number is " << MyNum << endl;
[COLOR=blue]return[/color] 0;
}
Ian