hi, i am using DEV-C++ and i have two problems with this code:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main (char argc)
{
    
      string n1;
      getline(cin,n1);
      
      int len;
      len = n1.length();
      
      int len1 = -2;
      int len2 = -1;
      
      
      for(int t = len; t>0; t--)
      {
              len1 = len1 + 2;
              len2 = len2 + 2;
              cout << "number "<< n1[len1] << endl;
              cout << "operator "<< n1[len2] << endl;
      }
      
       
      
      system("pause"); 
}

i want the output to never end with an operator and whenever i enter a number larger than 9, it ends with an operator. my second problem is after i enter an equation (like 1+1), the output is 1+1 and then it outputs just some weird gibberish. and here i have some other code:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main (char argc)
{
    
      string n1;
      getline(cin,n1);
      
      int len;
      len = n1.length();
      
      int len1 = -2;
      int len2 = -1;
      
      
      for(int t = len; t>0; t--)
      {
              len1 = len1 + 2;
              len2 = len2 + 2;
              cout << "number "<< n1[len1] << endl;
              cout << "operator "<< n1[len2] << endl;
              
              if(n1[len2] == "+")
              {
                          cout << n1[len1] + n1[len1] << endl;
              } //didnt want to put "-,/,*" yet
      }
      
       
      
      system("pause"); 
}

and the problem with this code is that i keep getting this error 25 F:\random\test.cpp ISO C++ forbids comparison between pointer and integer. any solutions?

In line 23 if I enter 100+99 why would the second character in the string be an operator?

Line 25 in the second i believe chars are compared with '' not ""

edit: that's single not double quotes

(though i think this project may be flawed at the planning stage)

Edited 5 Years Ago by frogboy77: n/a

In line 23 if I enter 100+99 why would the second character in the string be an operator?

Line 25 in the second i believe chars are compared with '' not ""

edit: that's single not double quotes

(though i think this project may be flawed at the planning stage)

Well, I just need the program to detect whole numbers.

And, on line 25 I replaced == with != and it still won't compile. Also, i would rather have the program output 13+900 instead of it outputting each digit on a new line.

What's wrong with this?

#include <iostream>

using namespace std;

int main()
{
    double number1, number2;
    char operation;

    while (true)
    {
        cin >> number1;
        cin >> operation;
        cin >> number2;

        if (!cin) break;

        switch(operation)
        {
            case '+': cout << number1 + number2 << endl; break;
            case '-': cout << number1 - number2 << endl; break;
            case '*': cout << number1 * number2 << endl; break;
            case '/': cout << number1 / number2 << endl; break;

            default: cout << "invalid operation character" << endl;
        }
    }

    return 0;
}

That calculator only lets you input one operator but i want mine to be able to let you input unlimited operators.

I just want to create my own calculator with the amount of knowledge I have on c++. thanks for the help though.

Ok, I'll try to keep it simple this time. Let's start with something easy -> A calculator able to perform only addition and subtraction.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string expression;

    double result, number;
    char operation;

    while (true)
    {
        getline(cin, expression);

        if (expression == "") break;

        stringstream buffer(expression);

        buffer >> result;

        while (buffer >> operation >> number)
        {
            switch(operation)
            {
                case '+': result += number; break;
                case '-': result -= number; break;
            }
        }

        cout << result << endl;
    }

    return 0;
}

While there is available input, you get it and perform the appropriate operation. I believe this doesn't need more explanation.

If you're not familiar with stringstreams, it's a good idea to spend some time playing with them. Basically,
they provide an interface through which you can manipulate strings as if they were input/output streams.

Now, let's try to add multiplication and division to the above...

//...

while (buffer >> operation >> number)
{
    switch(operation)
    {
        case '+': result += number; break;
        case '-': result -= number; break;
        case '*': result *= number; break;
        case '/': result /= number; break;
    }
}

//...

2 * 5 + 1 evaluates to 11 , as we want. However, 1 + 2 * 5 evaluates to 15 .
The problem here is that our calculator lacks the concept of operator precedence.

A possible solution to this is to evaluate the expression in multiple steps - in this case, two. During the
first step, we will perform multiplications and divisions and during the second, additions and subtractions.
That is, input like 1 + 2 * 5 - 3 * 4 + 6 will first be turned into 1 + 10 - 12 + 6 and then into 5 .

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

double eval_L1_exp(const string & expression)
{
    double result, number;
    char operation;

    stringstream buffer(expression);

    buffer >> result;

    while (buffer >> operation >> number)
    {
        switch(operation)
        {
            case '+': result += number; break;
            case '-': result -= number; break;
        }
    }

    return result;
}

string L2_exp_to_L1_exp(string expression)
{
    double operand1, operand2;
    char operation;

    expression += '#';

    stringstream buffer(expression);

    stringstream result;

    buffer >> operand1;

    while (buffer >> operation)
    {
        switch(operation)
        {
            case '+': case '-':
                result << operand1 << operation;
                buffer >> operand1; break;

            case '*': buffer >> operand2; operand1 *= operand2; break;
            case '/': buffer >> operand2; operand1 /= operand2; break;

            case '#': result << operand1; break;
        }
    }

    return result.str();
}

int main()
{
    string L1_expression;
    string L2_expression;

    while (true)
    {
        getline(cin, L2_expression);

        if (L2_expression == "") break;

        L1_expression = L2_exp_to_L1_exp(L2_expression);

        cout << L1_expression << endl;

        cout << eval_L1_exp(L1_expression) << endl;
    }

    return 0;
}

I believe the implementation of L2_exp_to_L1_exp is pretty straightforward too.
You shouldn't have trouble understanding how it works if you take a close look.

So, are we done yet? Well, not exactly... We still have to implement powers (^ operator), grouping
(using parentheses) and input validation. But I think I'll leave that stuff as an exercise to you (^_^)

Edited 5 Years Ago by m4ster_r0shi: n/a

This article has been dead for over six months. Start a new discussion instead.