Detect country's name via its area code

Q. Write a program which asks the user to enter a bunch of numbers (Mobile numbers) and each number should be listed under specific categories (Country names) according to the very first 3 digits (area code)

For example, 111 US, 100 UK, 555 Spain.. Based on such information we should detect each number where it belongs to.

The thing is that they did not mention how many digits those numbers make. Which makes it hard for me to find a way other than the traditional one.
(assume each number consists of 5 digits and area code is 640)

if(number>= 64000 && x<64100)...

But that's not that case, and hence why I'm here seeking your help. Thanks.
> they did not mention how many digits those numbers make
irrelevant, you operate on the first three digits
you may not even treat them as number
Thats the big question how do i operate on the first 3 digits, my knowledge is very limited so pls give me a suggestion
Easiest is to use a string and string::substr
http://www.cplusplus.com/reference/string/string/substr/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int first_three_digits_of( unsigned long long number )
{
    // chop off digits on the right till only three digits are left
    while( number > 999 ) number /= 10 ;
    return number ;
}

int main()
{
    unsigned long long number ;
    while( std::cout << "number? " && std::cin >> number )
        std::cout << first_three_digits_of(number) << '\n' ;
}
Last edited on
Topic archived. No new replies allowed.