2
#include <iostream>
using namespace std;

int main()
{
    int n,t;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cin>>t;
        cout<<to_string(t).length();
    }
    return 0;
}

Test Cases:
Input-123 //Correct
Output-3

Intput-111111111111111111111111 //Incorrect
Output-10

Input-11111 //Incorrect
Output-1010

Stuti
  • 57
  • 1
  • 6
  • 3
    111111111111111111111111 probably won't fit in an integer. Try printing t after the cin – nicomp May 02 '20 at 00:21
  • Does this answer your question? [max value of integer](https://stackoverflow.com/questions/15004944/max-value-of-integer) – 273K May 02 '20 at 00:21
  • @S.M. the third one will fit tho – pm100 May 02 '20 at 00:22
  • 1
    Check the returned value of `(cin>>t)` or `cin.fail()`. After the second input `cin` is failed and does not perform read, so `t` is not changed and has old length 10. You get double output of 10: 1010. – 273K May 02 '20 at 00:23
  • Just use a 78-bit integer, and it'll work. – Eljay May 02 '20 at 00:27

1 Answers1

2

This is most likely caused by cin reading as much of an int as it can when you assign 111111111111111111111111 to t, note that 111111111111111111111111 goes beyond the capacity size for int, it even goes beyond the capacity of long long int. Then as for fixing your output, chain an endl statement to the end of your output statement as so:

cout << to_string(t).length() << endl;

If you insist on making your code work for integers that exceed capacity then you might want to check out some arithmetic libraries such as the GMP Arithmetic Library