0
Kinda need some help here
#include <iostream> using namespace std; int num[6]; char op; int main(){ for(int i=0;i<6;i++){ cin >> num[i]; cin >> op; } for(int i=0;i<6;i++){ switch(op){ case '+': num[i+1]=num[i]+num[i+1]; break; case '-': num[i+1]=num[i]-num[i+1]; break; default: cout << num[i]; return 0; break; } } cout << num[5]; return 0; } //examples: 1+2+3+4+5+6 //examples: 5-6-7-7-8-9 Can someone help me how to make it calculate addition and subtraction together like: 1+2-6+5-7...etc (same with multiplication and division)
3 Answers
+ 1
Apparently we can do this without using any array.
https://code.sololearn.com/cS7506j5QXtF/?ref=app
+ 3
Vicent Roxan revamp the input loop. Consider these problems:
- After every input number it tries to read an operator, whereas after the last number it does not have one. Try looping only 5 times, instead of 6, then add one more input that reads the final number.
- op would hold only the last operator. Each iteration replaces the previous value. Maybe op should be an array, too.
+ 1
I have an idea with '+' and '-'. But the idea of multiplication and division (from the comment), isn't all too easy cause there are operator precedence to consider.