C++ Program to convert the string into a floatingpoint number


Static typing is used in the C++. Variables must be defined with a specific type in order to write programmes. Inputs from the console or files must occasionally be read. In this case, the programme is given the string data. Special operations are necessary to transform them into other datatypes. This article will provide the C++ method for converting strings to floating point integers. A couple different methods can be used to accomplish this. Explore each of them separately.

Using stringstream in C++

Streams are a fantastic tool in C++. Filestreams, standard input/output streams, etc. are examples of these streams. Stringstream is a different stream that exists. This operates similarly to other streams by accepting a string as input. We must import the sstream header file in order to use the stringstream. The stream data can be retrieved using the insertion operator (>>) or extraction operator ().

Syntax

#include < sstream >
stringstream streamObject ( <a string input> );

To use the stream to read a specific type of input, the syntax will be like the below −

Syntax

<data type> variable;
streamObject >> variable;

Algorithm

Let us see the algorithm to understand how this works as a whole.

  • Take a string object x as input
  • Create one stringstream object say ss and pass x into the object
  • Create an floating point variable xFloat
  • Use insertion operator from ss to store floating point into xFloat

Example

#include <iostream> #include <sstream> using namespace std; float solve( string myString) { float x; stringstream ss( myString ); ss >> x; return x; } int main() { string aNumber = "3.14159"; float convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "6.5 more than the given number is: " << convNumber + 6.5 << endl; }

Output

The given number is: 3.14159
6.5 more than the given number is: 9.64159

It is obvious from this example that the number was retrieved from a string object. And because this is actual floating-point data, we can add 6.5 to itself in floating-point notation and display the result.

Using sscanf() in C++

A comparable approach (which also works in C) is to use the sscanf() function. This function accepts a character array as input and the format string, just as the standard scanf() function. Now it reads the requested value from the string and appends it to the variable that is pointed to by the variable's address. View the sscanf() function's syntax.

Syntax

scanf ( <a string input>, <format string>, address(s) of variable );

Algorithm

Let us see the algorithm to understand how this works as a whole.

  • Take a string x as input in C-like character array format
  • Use the sscanf() function with parameter x, the format string, and the variable address
  • The variable value will directly be stored from the sscanf() function.

Example

#include <iostream> #include <sstream> using namespace std; float solve( string myString) { float x; sscanf( myString.c_str(), "%f", &x ); return x; } int main() { string aNumber = "6.8"; float convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "2.5 more than the given number is: " << convNumber + 2.5 << endl; }

Output

The given number is: 6.8
2.5 more than the given number is: 9.3

The application is operating exactly as it did before, however there is something we must note. The C++-like string object is not supported by the sscanf() method. It requires a character array a la C. To achieve this, we turned the supplied string parameter into a character array akin to C using the c_str() method.

Using stof() in C++

Using the stof() method from the "string" header file is another quick and simple way to convert a string to an integer. This function transforms a string object into its corresponding floating point number after receiving it as input.

Syntax

#include <string>
stof ( <integer in string format> );

Algorithm

  • Take a string object x as input
  • xFloat = stoi( x )
  • return xFloat as floating variable from given string x.

Example

#include <iostream> #include <sstream> using namespace std; float solve( string myString) { float x; x = stof( myString ); return x; } int main() { string aNumber = "6.8"; float convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "2.5 more than the given number is: " << convNumber + 2.5 << endl; }

Output

The given number is: 6.8
2.5 more than the given number is: 9.3

Using atof() in C++

Although the atof() is also available in C, it is pretty comparable to the stof. The string can be submitted using character array format. By importing the cstdlib library, you can get to it. Otherwise, there is no real distinction. Let's check out the syntax.

Syntax

#include <cstdlib>
atof ( <floating number in character array format> );

Algorithm

  • Take a string object x as input as C like character array format
  • xFloat = atoi( x )
  • return xFloat as floating point variable from given string x.

Example

#include <iostream> #include <sstream> using namespace std; float solve( string myString) { float x; x = atof( myString.c_str() ); return x; } int main() { string aNumber = "8.9"; float convNumber = solve( aNumber ); cout << "The given number is: " << convNumber << endl; cout << "6.5 more than the given number is: " << convNumber + 6.5 << endl; }

Output

The given number is: 8.9
6.5 more than the given number is: 15.4

Conclusion

There are several ways to convert strings to floating point numbers. The first two methods, using stringstream and sscanf(), are the generic ways to convert a string to any datatype without changing anything else; the only thing that will change is the type of the final variable. These functions, stof() and atof(), are solely used to convert strings to floats. Other functions that convert into different datatypes are equivalent. Since they are C-based functions, sscanf and atof() do not take string objects. Before using them, we must use the c_str() function to turn our string into a character array.

Updated on: 19-Oct-2022

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements