Thread: Help with my RPN calculator using c++ #include <stack>

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, use std::string. Also, read up on stringstreams (#include <sstream>) to help in parsing from string to int. Using getline is good, but it does mean that you need another round of parsing to individual string tokens. Again, stringstream can help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #47
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <sstream>
    #include <stack>
    #include <string>
    
    void calculation(int &ope1, int &ope2, char &input);
    using namespace std;
    stack <int> a;
    
    int main()
    {
    	char str[100];
    
    	cin.get(str, 100);
    	cout << str << endl;
    	system ("pause");
    }
    Ok, if all of this code correct. Step by step first
    Last edited by evildotaing; 01-03-2012 at 03:45 AM.

  3. #48
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main ()
    {
    	 string post;
    	 
    
    	 cout << "Enter expression: ";
    	 getline (cin , post);
    	 
    	 
    	 system ("pause");
    	 return 0;
    }
    is this correct. I am unsure about <sstream>
    Last edited by evildotaing; 01-03-2012 at 04:17 AM.

  4. #49
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    #include <string>
    #include <stack>
    
    void calculation(int &oper1, int &oper2, char &chr);
    
    using namespace std;
    
    int main()
    {
    	int number;
    	stack <int> a;
    	char ch;
    
    	cout << "Enter expression: ";
    	while (cin >> number)
    	{
    		a.push(number);
    		while (cin >> ch)
    		{
    			
    			switch (number)
    			{
    				
    			case '+':
    				
    				a.pop();
    				a.push(number + number);
    				
    				break;
    			case '-':
    				a.pop();
    				a.push(number - number);
    				
    				break;
    			case '*':
    				a.pop();
    				a.push(number * number);
    				
    				break;
    			case '/':
    				a.pop();
    				a.push(number / number);
    				
    				break;
    			}
    			a.pop();
    		
    		}
    		cout << a.top() << endl;
    		
    	}
    
    	
    
    	
    	system ("pause");
    	return 0;
    }
    Give up on sstream. Still cant solve

  5. #50
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    OK, this is a pity post - 50 posts, and 2 weeks, but I don't think you've learnt a great deal.
    Code:
                case '+':
                    int num1 = a.top(); a.pop();
                    int num2 = a.top(); a.pop();
                    a.push( num1 + num2 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me to write a calculator in C++ using stack?
    By anglin1024 in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2009, 08:12 AM
  2. Cannot open include file: 'stack.h'
    By GSalah in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2007, 03:18 PM
  3. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. Stack Calculator Error
    By shane1985 in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2003, 02:43 PM