1

When running the code to solve for the counting operations the answer will always come out to 3 operations. Why does count always output 3 even when it does not fit the criteria?

#include <string>
#include <iostream>
using namespace std;

int main() {
    string input1;
    string input2;

    cout << "Enter first number: ";

    cin >> input1;

    cout << endl;
    cout << endl;
    cout << "Enter second number: ";

    cin >> input2;


    int count = 0;

    for (int i = 0; i < 3; i++) {


        int thing = (input1.at(i) + input2.at(i));
        if (thing > 9) {
            count++;
        }
    }

    cout << count;

    return 0;
}
  • It looks like you’re using `char`s as `int`s. The sum of two `char`s entered by the user is very likely to be > 9. If you’re interested in the numeric values of the input then you’ll have to _convert_ text to numbers. – Biffen Feb 17 '21 at 07:56
  • 1
    Because the code page value of the string content will ensure it always complies with the criteria you think it won't comply with. To that I turn you to [an ascii table](http://www.asciitable.com/). Btw, always include your test input *as part of your question*, especially when stating it is related to the functional workflow when it apparently is divergent of your expectations. Back to your issue, in short, because `'1'` isn't `1`, it's (very likely) `49` (unless you're using some non-ascii platform, in which case it likely still isn't `1`). – WhozCraig Feb 17 '21 at 07:57
  • 1
    "solve for the counting operations" does not explain what you are trying to achieve. Show example input and what you expect the output is supposed to be if your program were working correctly. – paddy Feb 17 '21 at 08:02
  • @Shea Cardona, if you are satisfied with the answer, please mark it as an answer that solved your query. Or if there is some confusion, let me know. – KL_KISNE_DEKHA_HAI Feb 17 '21 at 09:03

2 Answers2

2

There is internal typecasting being done here. From char to int as the result is required is in integer format and addition is being done. Therefore actually ASCII values are being added. For '0' it is 48. Therefore everytime the sum is greater than 9. And hence output is 3. The answer here will provide a better insight.

For your question this is a better way to do it. (subtract '0' from each digit char), therefore actually internally it becomes ASCII(digit) - ASCII(0), which will give you the actual digit

#include <string>
#include <iostream>
using namespace std;

int main() {
    string input1;
    string input2;

    cout << "Enter first number: ";

    cin >> input1;

    cout << endl;
    cout << endl;
    cout << "Enter second number: ";

    cin >> input2;


    int count = 0;

    for (int i = 0; i < 3; i++) {


        int thing = (input1.at(i)-'0') + (input2.at(i)-'0');

        if (thing > 9) {
            count++;
        }
    }

    cout << count;

    return 0;
}
KL_KISNE_DEKHA_HAI
  • 649
  • 11
  • 26
  • @Shea Cardona, if you are satisfied with the answer, please mark it as an answer that solved your query so that it will help other people with the same problem. Or if there is some confusion, let me know – KL_KISNE_DEKHA_HAI Feb 23 '21 at 05:19
-1

If you are doing mathematical operation you must use appropriate types for the variables. Since you have used strings as variable type , it will not give correct result as desired. For the given code to work try this.

int input1;
int input2;

For more examples on user inputs in c++