String to double C++ conversion

Asked

Viewed 721 times

3

I’d like to convert a string passed to type value double.

Example:

string expressao = "1+1";
double x, y;

Such that, x = expressao[0]; and y = expressao[1];

And the sum of x+y return 2 in response.

I’ve read some things about atof, but I couldn’t apply it properly.

Follow the code below:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

double resultado(string); // prototipo

int main()
{
    string expressao;

    cout << "Exemplo de Expressão:\n\n1+2\n\n";
    cout << "Insira uma expressão:";
    getline(cin, expressao);

    cout << expressao << " = " << resultado(expressao) << endl;
}

double resultado(string valor) {
    double x, y;
/*
    gostaria de recuperar apenas os valores numericos. Por exemplo:
    Se a minha entrada for 1+1
    gostaria repartir a entrada(1+1) em duas partes.
    1. valor[0] para recuperar o valor 1
    2. valor[1] para recuperar o outro valor 1 depois do sinal(+)
    3. em seguida gostaria de converter esses dois char(valor[0] e valor[1]) em valores do tipo double par assim, somá-los e retorná-los.
*/
    return x;
/*
    if (opr[1] == '+') { 
    return x+y;
    } else if (opr[1] == '-') {
        return x-y;
    } else if (opr[1] == '*') {
        return x*y;
    } else if (opr[1] == '/') {
        return x/y;
    } else {
        return 0.0;
    }
*/
}
  • I would like to double convert these two parts recovered through the string. double x, y; string expression = "1+1"; // input x = expression[0]; // value 1 which is in char y = expression[1]; // value 2 which is in char understands me?

  • Post the part of how you are recovering the parts, to facilitate.

  • Yes, you can see that the problem is very different from the initial question. Here’s how to format your question: http://answall.com/markdown

  • I posted a photo now to help. The goal is to catch two chars from a string, convert these chars into doubles and then add them up.

  • thanks for the tip Bacco... looking here.

  • 3

    Actually it is preferable the code in text than the printscreen, but reading this help makes it easy you adapt.

  • I couldn’t edit it all :/

  • 2

    Leave 4 spaces before each line of code, which is cool. It has the button { } that already does it automatically. Select the part that is code and press the { }

  • That’s it. Now I’ve got it

  • You can view on http://answall.com/posts/18807/revisions. who actually managed to edit. I hope you can do it next time.

  • Ahh... rsrs thanks bigown and Bacco :)

Show 6 more comments

1 answer

2


See if it’s something like this you’re looking for.

#include <sstream>
#include <iostream>

double resultado(std::string valor)
{
    std::istringstream iss(valor);
    int X, Y;

    iss >> X >> Y;
    return X + Y;
}

int main()
{
    double z = resultado("1+2");

    std::cout << z << std::endl;
} 

Ideone

  • Exactly that Ideone :).

  • Could you explain to me what happens to the signal(+) in the code? From what I see, it was possible to receive the two integers without the interference of the signal.

  • Ah, got it. He catches by flow. Thank you :)

  • 1

    @Sunstreaker Actually the sign is not ignored no. What happens in this example is that X reads "1" and Y reads "+2" which is a valid notation for a number. To see that he is not ignoring just change the signal to something that is not valid as a number, for example "1&2". In this case he won’t be able to read the second number. Example: http://ideone.com/hwfpUv

Browser other questions tagged

You are not signed in. Login or sign up in order to post.