Aman Rawat
Aman Rawat

Reputation: 247

Unexpected binary output

I'm writing a code to add the first and last integer of a number. But the output results in binary instead of integer. //Code

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

int main() {
    int t, n;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin>> n;
        string s = to_string(n);
        char first = s[0];
        char last = s[s.length() - 1];
        int a = first;
        int b = last;
        cout << first + last;
    }
    return 0;
}

output code

/tmp/ujHkZRfwZL.o
1
1234
101

Upvotes: 1

Views: 65

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

You should at least write

    int a = first - '0';
    int b = last - '0';
    cout << a + b;

Upvotes: 2

Related Questions