Char array to double ?

Where is the "+" and "5" ? it give me only first array. but I want to every array.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{

    char calculate[2];
    cin >> calculate;  // enter this=  2+5

cout << endl <<"double= " << atof(calculate); //char array to double
}

double= 2

This is not how the input works cin takes the character number and puts them in the array ie. When you type 2+5

calculate[0] = 2;
calculate[1] = +;


and then you have run out of space so bye bye 5.

atof takes 2+ and looses the + and leaves you just the 2. This is because it does not do the math for you. it only converts numbers.

what you would have to do is parse the string 2+5 and separate the numbers and operators. Do the math at which point you would probably be using a float or double value anyway.
Topic archived. No new replies allowed.