0

I want to check if Pesel number(string e) is 11 digits, problem is that after conversion to int, temp shows random values in my debug couts.

Example:

e=74090927433
temp=1076483401.

e=81111638872
temp=-492739752

code:

void setPesel(string e)
{
    cout <<"Correct value:"<<e<<endl;
    int digits=0;
    std::string copied = e;
    int temp = atoi(copied.c_str());
    cout <<"Wrong value:"<<temp<<endl;
    while(temp != 0)
    {
        temp = temp/10;

        digits++;
    }
    if (digits !=11)
    {
        pesel="Nie prawidlowy numer pesel";
    }
    else

        pesel=e;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270

3 Answers3

0

Ints in c++ can't contain more than 10 digits whatever variant of them you are using. You can see the ranges of the different types there: What range of values can integer types store in C++

It seems for the problem you are describing you will have to make sure to use a long long int. Though if only the length of the input interests you maybe checking it directly on the string would be even better!

lapostoj
  • 113
  • 1
  • 1
  • 8
  • 2
    I'm tempted to down vote this. There is nothing limiting a `int` from holding a number with more than 10 digits. – NathanOliver Sep 04 '18 at 20:51
  • 1
    "Ints in c++ can't contain more than 10 digits" you should be more accurate with such statements – Slava Sep 04 '18 at 20:52
  • @NathanOliver also, digits in *what base*? ;-) – Jesper Juhl Sep 04 '18 at 20:54
  • `int` has a specified *minimum* size (`>= short`). But no standards specified maximum. A 1024 bit `int` would be perfectly valid for an implementation to provide. – Jesper Juhl Sep 04 '18 at 20:57
  • @NathanOliver can you explain me what I am misunderstanding in the post I am referring to then? I won't pretend I am an expert in c++ so happy to learn more here! – lapostoj Sep 04 '18 at 21:00
  • 2
    @lapostoj The very first line is *The minimum ranges you can rely on are*. That means an `int` has to at least hold `[-32,767, 32,767]`. It can hold more though and often does on 32 and 64 bit systems. It is implementation defined what it actually holds but you can find out with `std::numeric_limits::max();`. – NathanOliver Sep 04 '18 at 21:03
  • "what I am misunderstanding in the post I am referring to then?" you misunderstand the difference btw cannot contain and you cannot rely it can. – Slava Sep 04 '18 at 21:06
  • I see, but in a case of someone relying on the standard library, they would have be able to use only values within this "minimum" range, right? Then ok you would use other libraries defining "more powerful types of int". But I agree my statement was incorrect in the formulation. – lapostoj Sep 04 '18 at 21:06
  • No. They would be able to use the range `[std::numeric_limits::min(), std::numeric_limits::max()]` – NathanOliver Sep 04 '18 at 21:07
  • 1
    "they would have be able to use only values within this "minimum" range, right?" - Only if they have *no knowledge at all* about the platform/implementation their code will be compiled/run on. And even in that case, a `static_assert` on the return value of `std::numeric_limits::max()` can easily guard against the code compiling on an implementation with a too small `int`. – Jesper Juhl Sep 04 '18 at 21:11
  • Ok! I think now it is clear! Thanks a lot guys. And next time I will be more careful in my answers... – lapostoj Sep 04 '18 at 21:13
0

It seems that int on your platform cannot hold such value (usually it is 32 bits and most likely your platform is not exception). Simplest solution is to avoid converting string representation of the number to binary value - you already have it represented in decimal format so just use std::string::length() or std::string::size(). If you need to validate/normalize just remove leading zeros and/or arythmetic sign, that should be still simpler than converting such big numbers to binary format:

void setPesel(string e)
{
     if ( e.length() != 11 )
        pesel="Nie prawidlowy numer pesel";
    else
        pesel=e;
}

or one liner:

void setPesel(string e)
{
     pesel = ( e.length() != 11 ? std::string("Nie prawidlowy numer pesel") : e );
}
Slava
  • 43,454
  • 1
  • 47
  • 90
0

You don't need to convert the string to an integer, then use the % operator.
This can be done using std::isdigit:

const size_t length = e.length();
bool all_digits = true;
for (size_t i = 0; i < length; ++i)
{
  if (!std::isdigit(e[i]))
  {
     all_digits = false;
     break;
  }
}
if (all_digits && (length == 11))
{
  std::cout << "Number has all digits and 11 total.\n";
}
else
{
  std::cout << "Test is either not a number or less than 11 digits.\n";
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154