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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include <iostream>
#include <vector>
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<int> iv; // for holding our numbers that we are going to calculate
        vector<char> 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 ?
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        // ...
        vector<int> iv; // for holding our numbers that we are going to calculate
        vector<char> 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.