Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the numeric value of a string (general C++)

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
0
0
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.
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
 
there is already a defined function
Code:
int main
{
    char MyString[]="1.45";
    float MyNum=atof(MyString);
    cout << "The number is " << MyNum << endl;
    return 0;
}


"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
chpicker said:
The program needs to be cross-platform, so I have to stick to ANSI C++
If your program needs to be cross-platform compatible and you want to stick to ANSI-C++, you should consider using the standard library headers (e.g. <iostream> instead of <iostream.h>). The outdated header you used is not standard and therefore not necessarily consistent across platforms. It is also not even provided by many modern compilers (like Microsoft's VC++ .NET 2003).
 
look at function like atoi (ascii to integer) and itoa (integer to ascii).. just type them in google and you'll get a bunch of links which show you how to use them, parameters etc...

basically, atoi converts ascii to int :
Code:
char * a="11";
int s=atoi(a);

hope that helps
 
uolj said:
The outdated header you used is not standard
Ahh...that's going to take some getting used to. I've always used <iostream.h>, I thought that was the right one to use.

The books I've learned my C++ from all use the .h versions of the header files. Is this something that changed recently or are the books I'm using teaching me the wrong things? Why would there be 2 different headers, one with the .h and one without?

Ian
 
The books I used were pretty confusing - they used iostream sometimes and iostream.h sometimes and never explained the difference or why they used the one they did. Basically, the books are teaching you the wrong things. At some point they created this big namespace std and put the standard library inside it. The .h files were probably left for backwords compatibility but all new programs should use the standard version, which does not have the .h. Files that are left over from C (such as math.h and string.h) are now prefixed with a c (so its <cmath> and <cstring>). The only big difference is that you now have to specify that cin and cout and such are in the std namespace. You can do this in one line by saying "using namespace std;" although some people say that's a bad programming practice, it has never been an issue for me. Other options are having a line like "using std::cin" for everything you use in STL, or just putting std:: before everything in STL every time you use it.
 
The one with the .h is from before the C++ Standard was released. It is, therefore, not standard.

The one without the .h is as specified in the Standard. It is, therefore, standard.

In general, none of the C++ Standard header files end in .h anymore.

Standard C header files to be used in C++ programs should be renamed by removing the ".h" and prepending a "c". For example, instead of including "stdio.h", you'd include "cstdio".

The main difference you'll notice between the C versions of files and the new C++ versions is that things will be in the namespace [tt]std[/tt] instead of in the global namespace.

Incedentally, that's also the main difference you'll notice between iostream.h and iostream. There are a few "real" differences, too, but you probably won't come across them.

And unfortunately, yes, many books and tutorials are outdated and tell you to use iostream.h. Ignore them.


Also, if you want to only use standard functions, don't use [tt]itoa[/tt] as suggested above; [tt]atoi[/tt] is standard, but not [tt]itoa[/tt] (for whatever reason).


The most standard C++ way of doing string to number conversions (not including using C functions) involves using a stringstream to perform "input" from a string into an integer.

You might want to check out the source code for [tt]lexical_cast[/tt] at which does what you want in a much more general way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top