0

I was solving ArithmaticII. I am getting the correct output for the below input:

4
1 + 1 * 2 =
29 / 5 =
103 * 103 * 5 =
50 * 40 * 250 + 791 =

Output:

4
5
53045
500791

I am getting the correct output, but when I submit my solution to spoj, I get a SIGABRT runtime error.

Note: It may also contain spaces to improve readability.

Since the input might not contain spaces, how can I handle that, because that is giving me error.

because my program stops (runtime error) when I don't provide space in the input (1 * 1+2=)

terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoll

Please help. What should I do?

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main() {
    int t;
    string str;
    cin >> t;

    while (t--) {


        ///using cin.ignore() as input as preceded by a single line  
        cin.ignore();
        getline(cin, str, '\n');
        stringstream split(str);
        ///now use getline with specified delimeter to split string stream
        string intermediate;
        int flag = 0;
        long long int ans=1;
        while (getline(split, intermediate, ' ')) {
            if (intermediate == "=") {
                cout << ans<<"\n";
                break;

            }
            if (intermediate == "*") {
                flag = 1;
                continue;
            }
            else if (intermediate == "/") {
                flag = 2;
                continue;
            }
            else if (intermediate == "+") {
                flag = 3;
                continue;
            }
            else if(intermediate == "-"){
                flag = 4;
                continue;
            }
            if (flag == 1) {
                ans *= stoll(intermediate);
            }
            else if (flag == 2) {
                ans /= stoll(intermediate);
            }
            else if (flag == 3) {
                ans += stoll(intermediate);
            }
            else if (flag == 4) {
                ans -= stoll(intermediate);
            }
            else if (flag == 0) {
                ans = stoll(intermediate);
            }
        }
    }
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
akashking
  • 55
  • 1
  • 6
  • Rather use `std::istringstream` in combination with the extraction `operator>>()` to parse a line further for specific types expected. Whitespace characters will be ignored already. – πάντα ῥεῖ Mar 02 '19 at 12:19
  • https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c , https://stackoverflow.com/questions/19767644/c-parsing-input-string-to-variables , https://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c – πάντα ῥεῖ Mar 02 '19 at 12:29
  • 1
    If your input doesn't have to contain space then don't use `getline`. Normally in this kind of problem you read the input one character at a time. Use `get` for that. – john Mar 02 '19 at 13:02

1 Answers1

0

Take input one line at a time. Put the first number into ans. Then loop through the rest of the string chararcter by character. If the character is an arithmatic operator ('*' or '+' or '/' or '-') then there will be a number after it.extract the number and perform specified operation. If the character is '=' print the answer.

Hint:how to extract a number?

1.the first number starts from beginning and goes till the first arithmatic operator.
2. All other numbers are between arithmatic operators or '='.

priojeet priyom
  • 716
  • 5
  • 16