Converting string/char into integer

I've spent countless hours googling how to do this. I've read many solutions and still confused. Most of the solutions deal with an automatic input into the variable string, for example string test = "123". I want a prompt to appear asking the user to input a string or char and it will convert it to a integer for example a phone number. I tried using atoi but it returns 0. I tried stingstream convert but get an error. This is what I got so far: It converts it into ASCII but they give a double digit for each letter. Is there a easier way?? P.S. the cin.get function is suppose to draw only 7 char/string out of the user input and convert it into a phone number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
    char letters[9999];
    int length = strlen(letters);
    int num2[length];

    cout << "Please enter a telephone number expressed in letters" << endl;
    cin.get(letters,8);
    //cout << num2;

    //cout << atoi(letters);

    for(int i=0; i < 7; i++)
    {
        num2[i] = letters[i] - 48;
        //cout << num2[i];
    }

    for(int i=0; i < ; i++)
    {
        cout << num2[i];
    }
    cout << endl;
    return 0;
}
The functions ToString() and FromString() at http://ideone.com/WffuF work just fine as you can see from the demo, so long the entered string correctly represents a number (or in general, correctly represents data from the specified data type).
This code

1
2
3
    char letters[9999];
    int length = strlen(letters);
    int num2[length];


is incorrect. First of all length can have any value because array letters was not initialzed.
Secondly C++ does not allow ro define varable length array. So the definition of array num2 is incorrect.
Topic archived. No new replies allowed.