Broad Network


Numeric Conversion Functions in C++

String in C++ Standard Library Simplified - Part 6

Forward: In this article, I explain some functions that convert strings to numbers in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 6 of my series, String in C++ Standard Library Simplified. In this article, I explain some functions that convert strings to numbers in C++. Many inputs to the computer are in the form of strings. So you need functions that will convert strings to numbers (when numbers are inputted are strings).

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The C++ cstdlib Header
You need the cstdlib header file to convert strings to numbers. Each of the functions here takes a string with constant content as argument. So the content of the string we want to convert to number should be made const. A char* pointer proceeded by the keyword const points to a string whose content is constant. A string literal in quotes has a constant content. In this part, functions (not methods) of the cstdlib header file are used. I will give you just two functions (there are others). Each of the functions here takes one argument. The string header file can be used for any string object that you want. However, the functions do not take string objects. So the string literal from any string object has to be obtained as a string with constant content, if you want the literal of the string object.

The atof Function
The syntax is

    float atof(const char *nptr);

It converts a string with constant content (const char *nptr) to a number of type, float.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
    {
        const char *strLPtr = "2504.368";
        string str = strLPtr;

        const char *ptrChar = str.c_str();

        float number = atof(ptrChar);
    
        cout << number;

        return 0;
    }

If you tried the code, you would have noticed that some rounding took place for the result (do not worry about that for now). The first statement produces a string with constant content. Note the nature of the right and left hand sides to = of the first statement. The second statement assigns the pointer of the string with constant content to a string object. The third statement does the reverse of the first two statements, using the string class str_c method. We saw the str_c method in one of the previous parts of the series. The fourth statement converts the string with constant content to a float number, using the atof function.

The first three statements are not necessary for the operation of the atof function. However, you will meet them in programming, in different forms, related to the atof function. You can use a string literal directly as argument to the atof function. The following code illustrates this (read and try it):

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
    {
        cout << atof("2504.368");

        return 0;
    }

The atoi Function
The syntax is:

    int atoi(const char *nptr);

It converts a string with constant content (const char *nptr) to a number of type, int. Try the following:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
    {
        cout << atoi("352");

        return 0;
    }

There are more functions on this topic; but for this simple tutorial we shall end here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message