How do I fix my calculater | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I fix my calculater

Hello! I've been working on a 1 input calculater so you'll be able to do for example 7*5+3 and it will soulve it currectly I have a code, that code only works if you do for example 7*5*3 or 7+5+3+4 or 7-7-2-3 it only works if you do 1 oprator on it I'd really like someone to help me and fix it so you'll be able to do more oprations not just one this is the code: // calculater 1.0 // works only with + // made by Raz #include <algorithm> #include <iostream> #include <string> #include <sstream> using namespace std; int main() { string numbers = ""; int first = 0; int second = 0; int total = 0; int count = 0; int partplus = 0; int totalplus = 0; int totalminus = 0; int totalmulti = 1; int totaldev = 0; int third = 0; string str; cout << "Please enter the numbers " << endl; // 15+32 getline(cin, numbers); for (int i = 0; i < numbers.length(); i++){ str = str + numbers[i]; // 5+3-2+5 // 8,1,3 int first = stoi(str); int second = stoi(str); int third = stoi(str); if (numbers[i] == '+') { totalplus = totalplus + first; // 0=0+5 str = ""; } if (numbers[i] == '-') { count++; if (count == 1) { second = second*-1; } totalminus = totalminus-second; str = ""; } if (numbers[i] == '*') { totalmulti = totalmulti * third; str = ""; } } // plus case its just totalplus so its just in plus first = stoi(str); second = stoi(str); third = stoi(str); totalplus = totalplus + first; totalminus = totalminus -second; totalmulti = totalmulti * third; cout << totalplus<< endl; cout << totalminus<<endl; cout << totalmulti << endl; // can not be more than 11 numbers at ones }

13th Jul 2019, 12:06 PM
Cout
Cout - avatar
8 Answers
+ 1
Gordie what do you mean by printing the lexemes
13th Jul 2019, 8:09 PM
Cout
Cout - avatar
+ 1
Gordie you mean extract the numbers and the operators so I'll know what is what?
13th Jul 2019, 8:41 PM
Cout
Cout - avatar
+ 1
Gordie well, I posted a code the other day that can detect numbers in a string like 23 34 435 instead of spaces I could use + this is what I used in my code that's the code https://code.sololearn.com/c4vxY2YV7Gcl/?ref=app
13th Jul 2019, 8:56 PM
Cout
Cout - avatar
+ 1
Gordie for sure, whitespaces is like this right? " " By the way is there any way I can call you and you can help me after I am done with this?
13th Jul 2019, 9:19 PM
Cout
Cout - avatar
+ 1
Gordie why did you delete your last post?
13th Jul 2019, 10:21 PM
Cout
Cout - avatar
+ 1
Gordie but you see, the code that I gave you detects operators and numbers it's like if you see "+" for example so what it does it splits the numbers so what I tried to do in my code thay didn't work is when you see "+" i am adding it to the total +the number which you just input and that code didn't work
14th Jul 2019, 6:02 AM
Cout
Cout - avatar
+ 1
Gordie I understand, thank you I'll update you in the future on what's going on etc so I'll need to make a stack for it right? To do like * before the + etc
14th Jul 2019, 7:22 AM
Cout
Cout - avatar
+ 1
Gordie Got it, thanks
14th Jul 2019, 7:34 AM
Cout
Cout - avatar