12-3 C++ Converting Strings to Numbers

Because strings can store alphabethic or numeric symbols, character arrays sometimes store numbers. For example, numeric data read from files must sometimes be read into a character array. To perform math operations on numbers stored in a string, the number must be converted to a numeric data type.

The atoi function, which is short for aphabetic to integer, takes a string as a parameter and returns a value of type int. The number of feet in a mile and the number of miles are originally in character arrays in the following program.The atoi function is called twice to convert the strings to integer values.


#include <iostream>
#include <stdlib>

void mail()
{
 char feet_in_a_mile[] = "5280";
 char number_of_miles[] = "3";
 int feet_per_mile, miles;

 feet_per_mile = atoi(feet_in_a_mile);
 miles = atoi(number_of_miles);

 cout << "There are " << feet_er_mile * miles << " feet in " 
      << miles << " miles.\n";
}

The functions that convert strings to numbers require that the compiler directive #include appear at the beginning of the program.

When converting a string to an integer, the number in the string must be within the range allowed for the variable type to which you are converting. To convert a string to a long integer, use the atol function. Use it just like atoi. Assign the value returned by the function to a variable of type long.

Converting a string to a floating-point number is performed with the atof (alphabetic to floating-point function. The function accepts a string as a parameter and returns a value of type double. The statement below is an example of how the atof function can be used.

price = atof(mystring);

Like the functions that convert stgrings to integers, atof requires that the compiler directive #include appear at the beginning of the program.

Questions:

1. What function converts a string to a floating-point number?

2. What do the functions that convert strings to numbers return if the string contains no numbers that the function can convert?

3. Write a statement that converts the number in the character array named A to a long integer.

4. Write a statement that converts the number in the character array named B to a variable type int.

5. Write a code segment that converts the numbers in the character arrays named multiplicand and multiplier to floating-point numbers and then multiplies them together.

Program:

Write a program that allows the user to enter checks to be written against a checking account. Give the user a beginning balance of $1,000. Use an array of structure variables to store the following information for each check entered by the user: date, check number, who the check is payable to, and the amount of the check. Store the date in a character array.

Have the program ask the user to enter three checks. After each check, display the user's balance. After three checks have been entered, print the information entered in the array of structure variables back to the screen in a format of your choice.