0

I am trying to build a program that will read in a user defined string for example: "1x + 2y = 3", and take the numeric values 1,2 and 3 and store them into an array or a vector. so myArray would contain [1,2,3]; I am ultimately trying to build a program that will find the intersecting point of given lines using a matrix approach. But I need to be able to correctly store the input in a vector before I can work on that. My current code is listed below, but gives garbage values. I also not only need to convert the characters of the string into a double, I also need to to convert more than one character at a time, for example if the input number was 49.5, i need myArray[0] to hold 49.5, not just 4. I have tried using stod, but it gave me an error message and I cannot figure out why as I copied the code posted as an example straight into my IDE. Any suggestions are much appreciated :)_thanks in advance.

Here`s my code so far:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string sentance;
    int myArray[20] = {},
    arrayPosition = 0;

    getline(cin,sentance);
    for(int i = 0; i<sentance.length(); i++) {  
        if(isdigit(sentance[i])) {
            double tempDouble = sentance[i];

            myArray[arrayPosition] = tempDouble;
            arrayPosition++;
            cout<<"\ntempDouble is equal to: "<<tempDouble<<endl;
        }
    }

    cout<<"\n\nThe new array is:";
    for(int i = 0; i<= 20; i++) {
        cout<<myArray[i]<<"  ";
    }

    return 0;
}
  • 3
    Possible duplicate of [How can I convert string to double in C++?](http://stackoverflow.com/questions/392981/how-can-i-convert-string-to-double-in-c) – Shawn Mehan Oct 15 '15 at 00:18
  • Perhaps try atof when assigning to the tempDouble? (http://www.cplusplus.com/reference/cstdlib/atof/) – kvr Feb 29 '16 at 17:28

0 Answers0