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
#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
}
elseif (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;
}
// ...
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> .