Like Tree1Likes

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

This is a discussion on Help with my RPN calculator using c++ #include <stack> within the C++ Programming forums, part of the General Programming Boards category; Yes, use std::string. Also, read up on stringstreams (#include <sstream>) to help in parsing from string to int. Using getline ...

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 02: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 03: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 4 of 4 FirstFirst 1234
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, 02: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, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21