-1

I am trying to write a RPN calculator in which this line of input for a simple example: 2 3 + would print: 5 and then end.

I need the program to take the line of input, put the numbers on the stack, find non-numbers, check if they are operators: '+', '-', '/', or '*' and if they are then they compute the calculation for the last two numbers on the stack, remove those two numbers, and then add the new number onto the stack. This needs to go left to right, parsing through the line of input. Also if the symbol is not one of the operators it should print to cout.

Currently the program dumps a very long list of error code onto the screen when compiling.

Here is what I have:

#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main()
{
 stack<int> num;

 string line;
 int n,count=0,a,b;
 char c;


 while (getline(cin,line))
 {
     for (string::const_iterator it = line.begin(); it != line.end(); ++ it)
     {
         if (isdigit(static_cast<unsigned char>(*it)))
         {
             cout << it << endl;
             n = (it - 48);
             num.push(n);
             count++;
         }
         else if (ispunct(static_cast<unsigned char>(*it)))
         {
             if (it == '+' || it == '-' || it == '/' || it == '*')
             {
                 cout << "count is " << count << endl;
                 if (count>1)
                 {
                    b =  num.top();
                    num.pop();
                    a = num.top();
                    num.pop();

                    if (it == '+')
                    {
                        cout << "+" <<endl;
                        num.push(a+b);
                        count--;
                    }
                    else if (it == '-')
                    {
                        num.push(a-b);
                        count--;
                    }
                    else if (it == '/')
                    {
                        if (b != 0)
                        {
                            num.push(a/b);
                            count--;
                        }
                        else
                        {
                            cout << "division by zero" << endl;
                            return(0);
                        }
                    }
                    else if (it == '*')
                    {
                        num.push(a*b);
                        count--;
                    }
                    else
                    {
                        cout << "invalid input" << endl;
                        return(0);          
                    }
                 }
                 else
                 {
                     cout << "stack underflow" << c << endl;
                     return(0);
                 }
             }
            cout << c << endl;
     }
    }
 }
 while ( !num.empty() )
 {
     cout << num.top() << endl;
     num.pop();
 }
 return 0;
}
  • So does the program work? What is your question? – bames53 May 30 '14 at 18:35
  • Compiler dumps errors onto screen – user3678093 May 30 '14 at 18:36
  • 1
    You need to include the appropriate headers, and also `using namespace std;` (if that is your want). – ooga May 30 '14 at 18:36
  • "compiler dumps errors onto screen" like what errors? – Ben May 30 '14 at 18:37
  • Edit you question to show the headers. We're not mind-readers! :-) – ooga May 30 '14 at 18:38
  • You're missing the `` header for `isdigit` and `ispunct`. – ooga May 30 '14 at 18:40
  • a page long list of error I can't comprehend here is a small piece: In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:67:0, from /usr/include/c++/4.8.2/bits/char_traits.h:39, from /usr/include/c++/4.8.2/ios:40, from /usr/include/c++/4.8.2/ostream:38, from /usr/include/c++/4.8.2/iostream:39, from rpn.cpp:2: /usr/include/c++/4.8.2/bits/stl_iterator.h:805:5: note: template bool – user3678093 May 30 '14 at 18:40
  • @user3678093 this error message tells exactly nothing, put the whole output on some pasie website and provide a link – Valerij May 30 '14 at 18:41
  • You can edit your question to add information. – bames53 May 30 '14 at 18:52
  • https://docs.google.com/document/d/1Kuq2aWIEq5IdCxlcyFc76QGWv_roT_KtMHq4BoWwEjM/edit?usp=sharing – user3678093 May 30 '14 at 18:56

3 Answers3

3

Compiler dumps errors onto screen

The general method of handling this is to read the errors, understand the problem they're pointing out, and then to modify the program to correct the problem. If you don't understand an error message then one of the first things to try is to search the web for that specific error message or any specific terms in the error message that you don't understand.

If, after that research, you still don't understand a specific error message it may then be appropriate to ask other programmers for an explanation of that specific error message. In that case you will show them the specific error message and the lines of source code the error refers to.

Also, it's important to start from the beginning, with the first error listed. This is because earlier errors can confuse the compiler and lead it to produce more errors afterward that may not really make sense.


The document you posted with the error messages appears to have the beginning cut off. Are the error messages filling up the console buffer so that you're losing the beginning?

When I compile the code with gcc I get error messages starting with:

 main.cpp: In function 'int main()':
main.cpp:21:22: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
              cout << it << endl;
                      ^

If you start at the beginning then it's much clearer where to begin looking for the problem.

To prevent the compiler output from being cut off at the beginning you can do a number of things:

  • increase the buffer size or set it to unlimited if your terminal allows that
  • redirect the compiler output to a file or to a pager program, such as less.
  • use a command such as head -n 20 so that you only get the first few lines of output and the rest is discarded. After correcting whatever errors you see simply recompile to see any further errors.
bames53
  • 86,085
  • 15
  • 179
  • 244
1

A lot of the errors will probably go away if you replace it with *it wherever you want a char. Comparing a std::const_iterator to a character is probably what's causing your compiler to spew errors.

genisage
  • 1,159
  • 7
  • 17
1

You're missing the dereference operator (the asterisk) on most your uses of it.

And you're using c uninitialized.

And don't forget to add the <cctype header for isdigit and ispunct.

ooga
  • 15,423
  • 2
  • 20
  • 21