hi, i am trying to make a calculator and i have this code

#include<iostream>
#include<sstream>
using namespace std;
int main (char argc)
{
    int l = 1;
    for(l = l; l > 0; l--)
    {
      int q = -2;
      string n1 = "1+5";
      int n;
      int e;
      stringstream(n1[q=q+2]) >> n;
      stringstream(n1[q=q+2]) >> e;
      
      cout << n + e << endl;
    }
    system("pause");
}

this is just a test program i use to figure out hard problems with the original one. so if i dont use stringstream, the answer doesnt come out right. any solutions?

Lets see what your code does, and clarify it.

int l = 1;
    for(l = l; l > 0; l--)

The statement l = l is useless, it does nothing. Replace it with the previous line.

for(int l = 1; l > 0; l--)

Now, it should be pretty obvious that this just execute once, so it is equivalent to:

int l = 1;

But you actually never use l, so you can just get rid of it.

Now for the body of the loop:

int q = -2;
    string n1 = "1+5";
    int n;
    int e;
    stringstream(n1[q=q+2]) >> n;
    stringstream(n1[q=q+2]) >> e;
      
    cout << n + e << endl;

It should be clear that n1[q=q+2] just evaluates to n1[0] and then n[2] . Clearly, this takes the first character and gives it to a temporary stringstream object, then does the same for the third character. All the stringstream does is convert that character to a number. If you only need to convert one character to one number, then you can do this on your own simply like this:

int n = n1[0] - '0';
    int e = n1[2] - '0';

If you need to parse anything more complicated than a simple three-character string, you will need a lot more work.

At the end, of course, your program is equivalent to:

int main() {
  int n = '1' - '0';
  int e = '5' - '0';
  cout << n + e << endl;
  return 0;
};

Which, in turn, is equivalent to:

int main() {
  cout << 1 + 5 << endl;
  return 0;
};

I wanted to show this reduction to show that there is no special logic in your code, no parsing, it is just a circumvented way to print 1 + 5, but it cannot do anything else.

I think that the first thing you should look at is "tokenizing" which means to split up a string (or stream input) into a set of tokens (numbers, operators, parentheses, etc.). This is not something stringstream does automatically, you will have to either use a more powerful tool, or program your own tokenizer by extracting one character after another (with the help of stringstream).

no, i have the program do n1[q=q+2] to find all numbers and put them into different variables. And i use the for() statement to make sure that with it in my original program, i know it works.

>>i have the program do n1[q=q+2] to find all numbers and put them into different variables.

That is not what the program does. That's my point. All your program does is pick two characters and transform them into numbers. It does not find all numbers, it does not even search for any number, it picks it from a predetermined position in the string.

>>And i use the for() statement to make sure that with it in my original program, i know it works.

The for() loop is inconsequential, if something works outside a loop, it works inside too.

You need to focus your efforts on what is important. What is most important is to find a way to detect numbers (where they start, where they end), and be able to figure out whatever is between numbers (like +).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.