Thread: what could the problem be?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    what could the problem be?

    Ok when I run my program I test 23+. It suppose to be converted to 2+3 and the answer should be 5. But when I run it it gives me the answer 2. Im not sure what the problem is. Ive use the debugger but Im not figuring out why I get 2.

    Code:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    using namespace std;
    
    const int DefaultListSize =50;
    typedef float Elem;
    
    class Link {
    public:
    	Elem element;	/*Value for this node*/
    	Link* next;		/*Pointer to the next node in list*/
    	Link(const Elem& elemval, Link* nextval =NULL)
    	{ element = elemval; next = nextval; }
    	Link(Link* nextval =NULL) { next = nextval; }
    };
    
    class Lstack {
    private:
    	Link*top;	/*Pointer to first element*/
    	int size;	/*Count number of elements*/
    
    public:
    	Lstack(int sz =DefaultListSize) {top = NULL; size = 0;}
    	~Lstack() {clear();}	/*Destructor*/
    	void clear() {
    		while (top != NULL) { /*Delete link nodes*/
    			Link*temp = top;
    			top = top->next;
    			delete temp;
    		}
    		size = 0;
    	}
    	bool push(const Elem& item) {
    		top = new Link(item,top);
    		size++;
    		return true;
    	}
    	bool pop(Elem& item) {
    		if(size == 0) return false;
    		item = top->element;
    		Link*ltemp = top->next;
    		delete top;
    		top = ltemp;
    		size--;
    		return true;
    	}
    	bool topValue(Elem& it) const {
    		if (size ==0) return false;
    		it = top->element;
    		return true;
    	}
    	int length() const {return size;}
    	bool isEmpty() const {
    		if (top == NULL) return true;
    		else return false;
    	}
    };
    	bool Operand(float ch){
    		if(('0' <= ch) && (ch <= '9'))
    			return true;
    		else return false;
    	}
    
    	bool Operator(char ch){
    		if((ch == '+') || (ch =='-')|| (ch == '*') || (ch == '/') || (ch == '^'))
    			return true;
    		else return false;
    	}
    
    	float Eval(float oprndL, char oper, float oprndR);
    	
    int main(){
    
    	Lstack S;
    	string s;
    	float a = 0;
    	char b = 0;
    	float c = 0;
    	float answer;
    	unsigned int i;
    	
    	cout << "Enter Postfix string:\n";
    	getline(cin, s);
    
    	for(i=0; i < s.length(); i++ ){
    		if(s.at(i) != '\0'){
    			if(Operand(s.at(i))){
    				S.push(Operand(s.at(i)));
    			}
    			else if(Operator(s.at(i))){
    				b = s.at(i);
    			}
    		}
    	}
    	if(!S.isEmpty()){
    		S.pop(c);
    		S.pop(a);
    		Eval(a, b, c);
    		S.push(Eval(a, b, c));
    		S.pop(answer);
    		cout << answer << endl;
    	}
    	return 0;
    }
    
    float Eval(float oprndL, char oper, float oprndR){
    		if(oper == '+') return oprndL + oprndR;
    		else if(oper == '-') return oprndL - oprndR;
    		else if(oper == '*') return oprndL * oprndR;
    		else if(oper == '/') return oprndL / oprndR;
    		else if(oper == '^') return pow(oprndL,oprndR);
    		else return 0.0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because the only thing you're pushing onto your stack is "true". You need to push the value of the thing, not "true" or "false" like you get from Operand().

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    So you are saying I need an if statement?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have an if statement. I'm saying that this:
    Code:
    S.push(Operand(s.at(i)));
    is desperately wrong. You need to turn that character into a number, not into a bool.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Ok well I changed it to this:
    Code:
    S.push(s.at(i));
    But now here is whats weird. If you use subtraction the value comes out correct but if you use any other operator you wont get the right value.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to turn that character into a number. In your example:

    s.at(i) = 50 (ASCII '2')
    Operand(s.at(i)) = 1 (bool true)

    Neither of those is 2. So you're going to have to get 2 out of this character.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok so I need to add something somewhere that converts the character into a number is that correct? Or is it something Im doin wrong with like in post #4 where Im pushing a character and not a number?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to do something to turn '2' into 2, yes. If you can promise "always a single digit", then you can subtract '0' to get 2. If you have numbers >10, then you will have to work harder.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    ok I tried this but this does nothing. So is there any suggestions?

    Code:
    int main(){
    
    	Lstack S;
    	string s;
    	float a = 0;
    	char b = 0;
    	float c = 0;
    	float answer;
    	unsigned int i;
    	
    	cout << "Enter Postfix string:\n";
    	getline(cin, s);
    
    	for(i=0; i < s.length(); i++ ){
    			if(Operand(s.at(i))){
    				s.at(i) - '0';				
                         S.push(s.at(i));
    			}
    			else if(Operator(s.at(i))){
    				b = s.at(i);
    			}
    	}
    	if(!S.isEmpty()){
    		S.pop(c);
    		S.pop(a);
    		Eval(a, b, c);
    		S.push(Eval(a, b, c));
    		S.pop(answer);
    		cout << answer << endl;
    	}
    	return 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I've forgotten. Is it a stack of floats or a stack of chars? If it's a stack of chars, then you need to push the letter, and then subtract the '0' when you pop. If it's a stack of floats, you need to push the value of s.at(i) - '0' (as opposed to pushing the character as you are now).

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "s.at(i) - '0';" obtains the value of s.at(i), subtracts '0' from it, and discards the result. The net result is no visible effect.

    If you actually want to use the value of s.at(i) - '0' you need to do something with it, rather than just discarding it. For example, to save it in the variable b, use "b = s.at(i) - '0';". To print it out (assuming the value is printable) do something like "cout << (b.at(i) - '0');"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM