### Calculator will not add two or more digit numbers

closed account (EwCjE3v7)
Hello, I would like to know if anyone can fix this program for me, so it`s a calculator and the problem is that if I type
 1+1
. It works fine but when I type in
 10+10
, it give me the output of 1+0=1. I know why I`m having this problem but don`t know how to fix it. The problem is that I store all the numbers in an int vector and and the signs(+-*/) in a char vector and then add/subtract/multiply/divide as we process each number. I`m sure you can see the error once you run it

CODE:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` `````` #include #include using std::cout; using std::cin; using std::cerr; using std::endl; using std::vector; using std::string; int main() { string input; // for storing our input in cout << "Type your calculations, example \"5+5-2*6/4\" (Spaces are allowed) : "; // tell user how the calculator works while (getline(cin, input)) { // get inuut vector iv; // for holding our numbers that we are going to calculate vector cv; // for checking if user wants to +,-,*,/ if (!input.empty()) { // make sure we get input for (auto c : input) { // if we get input then check the characters in input if (isdigit(c)) { // if the character is a digit then iv.push_back(c - '0'); // place it at the end of the int vector iv } else if (ispunct(c)) { // if its a puntuation then if (c == '+' || c == '-' || c == '*' || c == '/') { // check if its a puntuation mark that we can process cv.push_back(c); // put it at the end of the char vector } else { cerr << "Sorry character/sign not known: '" << c << "'. Only +,-,*,/." << endl; // tell user that the character/sign is not known return -1; // return failiure } } } int answer = 0, counter = 0; // For holding the answer auto cvcbegin = cv.cbegin(), cvcend = cv.cend(); while (cvcbegin != cvcend) { while (*cvcbegin == '+') { if (counter == 0) { answer = iv[0] + iv[1]; cout << iv[0] << " + " << iv[1] << " = " << answer << endl; counter = 2; ++cvcbegin; } else { cout << answer << " + " << iv[counter] << " = " << answer + iv[counter] << endl; answer = answer + iv[counter]; ++counter; ++cvcbegin; } } while (*cvcbegin == '-') { if (counter == 0) { answer = iv[0] - iv[1]; cout << iv[0] << " - " << iv[1] << " = " << answer << endl; counter = 2; ++cvcbegin; } else { cout << answer << " - " << iv[counter] << " = " << answer - iv[counter] << endl; answer = answer - iv[counter]; ++counter; ++cvcbegin; } } while (*cvcbegin == '*') { if (counter == 0) { answer = iv[0] * iv[1]; cout << iv[0] << " * " << iv[1] << " = " << answer << endl; counter = 2; ++cvcbegin; } else { cout << answer << " * " << iv[counter] << " = " << answer * iv[counter] << endl; answer = answer * iv[counter]; ++counter; ++cvcbegin; } } while (*cvcbegin == '/') { if (counter == 0) { answer = iv[0] / iv[1]; cout << iv[0] << " / " << iv[1] << " = " << answer << endl; counter = 2; ++cvcbegin; } else { cout << answer << " / " << iv[counter] << " = " << answer / iv[counter] << endl; answer = answer / iv[counter]; ++counter; ++cvcbegin; } } } cout << "\nAgain?\n" << endl; } else { // If we get no input then... cerr << "No input, well bye." << endl; // Tell user that there was no input return -1; // return failiure } } return 0; } ``````
closed account (iAk3T05o)
It looks more complex than it should. Why are using `string input ` for a calculator and putting it in an int vector when it should be ` int input = 0 `?
 ``14151617181920212223242526272829303132333435363738394041424344454647`` `````` // ... vector iv; // for holding our numbers that we are going to calculate vector cv; // for checking if user wants to +,-,*,/ if (!input.empty()) { std::istringstream in(input); while (in) { in >> std::ws; // consume whitespace. int value; if (isdigit(in.peek()) && in >> value) iv.push_back(value); else { in.clear(); char ch; if (in >> ch) { if (ch == '+' || ch == '-' || ch == '*' || ch == '/') cv.push_back(ch); else { cerr << "Sorry character/sign not known: '" << ch << "'. Only +,-,*,/.\n"; return -1; } } } } int answer = 0, counter = 0; // For holding the answer // ... ``````

Your code has other problems. For instance the loops beginning on lines 31, 44, 57 and 70 result in undefined behavior when they increase cvcbegin and dereference it when it is equal to cvcend.

Your indentation is inconsistent. Also, you shouldn't be afraid of using a little whitespace to make your code more readable.

Your original code needs `#include <string> `. If you use the modified code above you'll also need `#include <sstream> `.
Last edited on
closed account (EwCjE3v7)
Sorry cire your code didn`t fix the problem, when i have it 10+5 its said Sorry character/sign not known.
closed account (EwCjE3v7)
Anyone who can help?
 Sorry cire your code didn`t fix the problem, when i have it 10+5 its said Sorry character/sign not known.

Really?
http://ideone.com/HZovSP
closed account (EwCjE3v7)
Sorry cire, your second code worked. Thank you. :)
Topic archived. No new replies allowed.