add and substract

Posted on 2003-02-25
Medium Priority
Last Modified: 2010-04-01
Follwing program converts string of int. to integers and adds them.  Can you add code to this program to substracts integers as well, if there is any nagetive number.

#include <iostream>
#include <string>

using namespace std;

int main()
    string s, container;
    int pos, x, x2, result = 0, nextpos, init = 0;

    cout<<"Enter expression to evaluate: ";
    cin >> s;
   pos = s.find('+',init);

        container = s.substr(init, pos);
        x = atoi( container.c_str() );
         result += x;
        nextpos = s.find('+',pos+1);
        if( nextpos == -1 )
              result += atoi( s.substr( pos+1, s.length()-1 ).c_str() );
         init = pos+1;
         pos = nextpos;


   return 0;

Question by:learncpp

Accepted Solution

Exceter earned 40 total points
ID: 8018534
>> Can you add code to this program to substracts integers as well, if there is any nagetive number.

No, but you can. Try it and come back when you run into trouble.

As I said in your last question in regard to this, this is a terribly complex way of evauating expressions. I suggest that you read up on Polish Notation.

To make this do subtraction you have to write code that will recognize which operator is at a given position and perform the appropriate action.

LVL 41

Expert Comment

by:Kyle Abrahams
ID: 8022721
*cough* HOMEWORK *cough*

Author Comment

ID: 8176302
int subs(string s, int initM){

     int result2 = 0;
     int  pos_minus, x2, count;
     initM = 0;
     pos_minus = s.find('-',initM);
     if (pos_minus >= 0)                        //enter the loop if - exits
          string container2;
          int pos_minus, next_minus,next_sign,pos_end;
          int length;                                     //first_minus;
          count = 0;

          pos_minus = s.find('-',initM);

          while (count<s.length())
               pos_minus = s.find('-',initM);
               next_minus = s.find('-',pos_minus+1);        
               next_sign = s.find('+',pos_minus+1);
               if( (next_minus == -1) || (next_sign == -1))
                    result2 += atoi( s.substr( pos_minus+1, s.length()-1 ).c_str() );

               if (next_minus < next_sign)                  
                    pos_end = next_minus;
                    pos_end = next_sign;

             length = pos_end - pos_minus+1;


             container2 = s.substr(pos_minus + 1,length);
               x2 = atoi(container2.c_str());
               result2 +=x2;

               initM = pos_end + 1;


   return result2;

//I got it.  Thanks

Featured Post

Technology Partners: 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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

579 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