[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Operations on Stack values

Posted on 2009-07-06
2
Medium Priority
?
216 Views
Last Modified: 2012-05-07
So I'm still new to c++ after doing java for a while and really am starting to miss a lot of the things I took for granted in Java haha.  Anyways, I'm working on a linked list implementation of a stack that gets input and does a certain function to that input.  Anything that isn't a certain word or an integer is to be ignored.  Integers are to be pushed onto the stack while a plus sign ("+") for instance sums the top two nodes of the stack and replaces the 2 values with that 1 value.  Or putting the word "Empty" in the input would empty out the stack.  There are other functions, but you get the idea.  

One of the problems I'm running into now are how to do math functions on the string input. Right now, I'm trying to convert the string to an integer and back to a string but I'm unsure of how to do the last part.  Another problem is how to ignore anything that I don't need, specifically to deal with identifying integers as a part of the string input.  Attached is a part of my code.  I think if I'm able to get one of the operations to work correctly,
int Stack::Plus(){ //Add the top 2 values in the stack, remove both of the values and insert their sum
	  StackItems first;  //StackItems is a struct in my header file with typedef string StackItemType item and StackItems *below in the function
	  StackItems second;
	  first.item = topDisplay(); topDisplay gets the top item
	  int topOne = ConversiontoInt(first.item);
	  pop(); //removes top item
	  second.item=topDisplay();
	  pop();
         int topTwo = ConversiontoInt(second.item);
	  int Sum = topOne+topTwo;
         //convert back to string here
	}

Open in new window

0
Comment
Question by:JamesL1518
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 24787710
In C++, you can leav3 most of the work to STL classes (http://www.sgi.com/tech/stl/), so in your case the conversion from int to string could be as simple as the attached code (also does a lot of the ignoring stuff)
#include <sstream>
 
string ConvertToString(const int n) {
 
  stringstream ss;
 
  ss << n;
 
  return ss.str();
}

Open in new window

0
 
LVL 7

Expert Comment

by:nikhilmenon
ID: 24787749
Well for the conversion of string to integer and vice versa, you could use:

atoi() and itoa() which perform string to integer and integer to string conversions respectively. This could help you as far as performing math operations on your string input is concerned.

FYI,

int atoi(const char *str) is the definition of the atoi() function.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question