1

I'm trying to make a program in C++ where the user will input an equation, (e.g.: y = 3x + 6). How would I determine what's a Value, and what's a Character, and where they are?

So from the example, I would know that:

  • Value 3 is at position stringArray[4]
  • Value 6 is at position stringArray[9]
  • Character x is at position stringArray[5]

How would I write this?

When I enter y = 3x in visual studio 2013, it comes up with an error box:

Unhandled exception at 0x77662EEC in Graphmatica_WhatsInDatInput.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x00FAF94C.

but when I enter anything that is not starting with a character, it's fine (e.g. 1 + 3x)

This is my code so far:

#include <iostream>
#include <string>

using namespace std;

int main(){

    string Equation;
    double dNumber;

    cout << ": ";
    getline(cin, Equation);


    for (int i = 0; i < Equation.size(); i++){
        if (isdigit(Equation[i])) {
            cout << "Number: ";
            dNumber = stod(Equation);
            cout << dNumber << endl;

        }
        else {
            if (Equation[i] != ' ') {
                cout << "Character" << endl;
            }
        }
    }

    cout << endl;
    system("pause");
}
Chait
  • 1,052
  • 2
  • 18
  • 30
user3502489
  • 361
  • 1
  • 4
  • 11
  • You decide which characters have meaning (i.e. are operators), which have no meaning (i.e. whitespace), which are literals (i.e. numbers) and which are variables (i.e. letters). Then you go through the string one character at a time, classifying the character accordingly and decide how it must be handled. Note: this doesn't mean you blindly execute things left to right: arithmetic has rules. But you will see that this is the same way *you* read equations: left to right, one character at a time. The computer just needs help and instructions on how to go about doing that. – Nik Bougalis Apr 06 '14 at 00:12
  • possible duplicate of [Infix expression evaluation](http://stackoverflow.com/questions/12122161/infix-expression-evaluation) – jterrace Apr 06 '14 at 00:13
  • I don't think this is a duplicate although that question offers insights both on how to parse expressions of this sort and how to go about solving this problem. This question involves the use of variables, and how to go about distinguishing them from other "things" in the expression. – Nik Bougalis Apr 06 '14 at 00:15
  • 1
    @user3502489 _'I get an error.'_ is too vague, please explain (dit your question again!). – πάντα ῥεῖ Apr 06 '14 at 00:23
  • Its edited! my error is this: Unhandled exception at 0x77662EEC in Graphmatica_WhatsInDatInput.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x00FAF94C. – user3502489 Apr 06 '14 at 00:40

1 Answers1

0

Your error is because of this line:

dNumber = stod(Equation);

Its exception documentation says: If no conversion could be performed, an std::invalid_argument exception is thrown.

When you entered a letter, no conversion could be performed and you got the exception.

Secondly, you're trying to convert the entire equation to a double.. If that is the case, what was the point of checking if each index in the string is a digit?

I think you meant to put: stod(to_string(Equation[I])) instead..

You can also create your own conversion functions using the ones already provided for you in C.

#include <iostream>
#include <string>

using namespace std;

inline double strtodouble(char c) noexcept
{
    return strtod(&c, NULL);
}

inline double strtodouble(const char* str) noexcept
{
    return strtod(str, NULL);
}

inline double strtodouble(const std::string str) noexcept
{
    return strtod(str.c_str(), NULL);
}


int main()
{

    string Equation;
    double dNumber;

    cout << ": ";
    getline(cin, Equation);


    for (int i = 0; i < Equation.size(); i++)
    {
        if (isdigit(Equation[i]))
        {
            cout << "Number: ";
            dNumber = strtodouble(Equation[i]);
            cout << dNumber << endl;

        }
        else
        {
            if (Equation[i] != ' ')
            {
                cout << "Character" << endl;
            }
        }
    }

    cout << endl;
    system("pause");
}
Brandon
  • 22,723
  • 11
  • 93
  • 186