-4

I want to find the length of an integer in C or C++ by using a switch statement I know there are other simple ways but I want use this method for some reasons so here is what I have tried so far:

#include <iostream>
#include <conio.h>

main() {
int a,length;
std::cout << "Please enter a number:\n";
std::cin >> a;
if(a<0)
    a=-a;
switch(a){
case 1<...<10:length==1,break;
case 10<...<100:length==2,break;
case 100<...<1000:length==3,break;
}
std::cout << "It has "<< length <<" numbers\n"; 
getch();    
}

So , I dont know what I can use instead of cases.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Majid Amiri
  • 580
  • 4
  • 14
  • Looks like incomplete homework for me. Take a look at http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm (for example) – arc_lupus Nov 24 '15 at 10:17
  • You can't switch on arbitrary comparisons, so you're going to have to want to do something else. A switch is essentially a sequence of equality comparisons. – molbdnilo Nov 24 '15 at 10:17
  • A switch is designed as a simplified version of the if-else statement using only direct comparisons. Thats why you can't use it like that. Use the normal if-else statements. – Magisch Nov 24 '15 at 10:20
  • @arc_lupus I always wonder why low-qualified programmers so bother that somebody tries to do his homework! Can you explain me this phenomenon? – Vlad from Moscow Nov 24 '15 at 11:29
  • @VladfromMoscow: If you target me with "low-qualified programmer": Yes, that is true, but I thought that SO is not a page where you ask things without first taking a look at the implementation and definition... – arc_lupus Nov 24 '15 at 12:23
  • @arc_lupus Maybe it would be more useful for you to spend your time learning C and C++ instead of bothering about questions of others. What do you think about this? – Vlad from Moscow Nov 24 '15 at 12:39

5 Answers5

3

If you are using the GCC extension, can be described as follows.

switch(a){
case 1 ... 9     : length = 1; break;
case 10 ... 99   : length = 2; break;
case 100 ... 999 : length = 3; break;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
2

It is tempting to use a series of if statements like

if (a >= 10000){
    length = 5;
} else if (/* And so on

But this requires you to make assumptions about the possible maximum size of an int. This is not defined by C or C++, although the minimum possible range is [-32767 to +32767].

Some compilers allow you to use a range-based switch but this is (i) not standard C or C++, and (ii) you'll still encounter the problem with possible range above.

A better approach is to repeatedly divide your int by 10 and count the number of times you can do that until you reach zero. And don't forget to test your work with a negative number and an initial value of 0.

Computing the base-10 logarithm is problematic due to the computation taking place in floating point.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You can use the following approach with the switch statement. Though in my demonstrative program I use only the label default you may include any case label that corresponds to the enumeration

#include <iostream>

int main()
{
    while ( true )
    {        
        enum : unsigned int { One = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine } digits;

        std::cout << "Enter a non-negative number (0-exit): ";

        unsigned int x = 0;
        std::cin >> x;

        if ( !x ) break;

        int len;


        switch ( ( digits = One,   x < 10 ) ||
                 ( digits = Two,   x < 100 ) ||
                 ( digits = Three, x < 1000 ) ||
                 ( digits = Four,  x < 10000 ) ||
                 ( digits = Five,  x < 100000 ) ||
                 ( digits = Six,   x < 1000000 ) ||
                 ( digits = Seven, x < 10000000 ) ||
                 ( digits = Eight, x < 100000000 ) ||
                 ( digits = Nine,  x < 1000000000 ), digits )
        {
                default:
                    len = digits;
                    break;
        }

        std::cout << x << " has " << len << " digits." << std::endl;
    }        
}

The program output might look like

Enter a non-negative number (0-exit): 1
1 has 1 digits.
Enter a non-negative number (0-exit): 11
11 has 2 digits.
Enter a non-negative number (0-exit): 111
111 has 3 digits.
Enter a non-negative number (0-exit): 1111
1111 has 4 digits.
Enter a non-negative number (0-exit): 11111
11111 has 5 digits.
Enter a non-negative number (0-exit): 111111
111111 has 6 digits.
Enter a non-negative number (0-exit): 1111111
1111111 has 7 digits.
Enter a non-negative number (0-exit): 11111111
11111111 has 8 digits.
Enter a non-negative number (0-exit): 111111111
111111111 has 9 digits.
Enter a non-negative number (0-exit): 0

An alternative representation of the switch including other case labels

    switch ( ( digits = One,   x < 10 ) ||
             ( digits = Two,   x < 100 ) ||
             ( digits = Three, x < 1000 ) ||
             ( digits = Four,  x < 10000 ) ||
             ( digits = Five,  x < 100000 ) ||
             ( digits = Six,   x < 1000000 ) ||
             ( digits = Seven, x < 10000000 ) ||
             ( digits = Eight, x < 100000000 ) ||
             ( digits = Nine,  x < 1000000000 ), digits )
    {
        case One:
            std::cout << "You could enter more digits.." << std::endl;
        case Two: case Three: case Four: case Five: case Six: case Seven: case Eight:
            len = digits;
            break;
        case Nine:
            std::cout << "Do you like money?" << std::endl;
            len = digits;
            break;
        default:
            std::cout << "Too big number! It can be never" << std::endl;
            break;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

you can't use switch for that purpose.Use if-else statements instead

#include <iostream>
#include <conio.h>

int main() {
    int a,length;
    std::cout << "Please enter a number:\n";
    std::cin >> a;
    if(a<0)
        a=-a;
    if( a >= 1 && a < 10 )
        length = 1;
    else if( a >= 10 && a < 100 )
        length = 2;
    /* .............. */

    std::cout << "It has "<< length <<" numbers\n"; 
    getch();    
}
machine_1
  • 4,266
  • 2
  • 21
  • 42
  • i said i knew there are other ways and i knew how to use `if-else` statement but i wanted to know if there is any way i can use `switch` statement ... tnx for ur help – Majid Amiri Nov 24 '15 at 10:33
0

You cannot use comparisons in switch case. I would recommend a series of if else if in this case with progressively lower limits:

if (a >= 100) length = 3;
else if (a >= 10) length = 2;
else length = 1;

Add as many as needed for the number of digits you want to handle.

Also note you have to assign to length. When you are doing length==1 you are returning a boolean value.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • @machine_1 and for your specific comment the OP seems to be interested only for numbers with no more than 3 digits. According to the code in his question. Of course the series of if-else need to cover the desired range whatever that is. – Manos Nikolaidis Nov 24 '15 at 10:31