简体   繁体   中英

Why am I getting 32767 after each output in the following code?

const bool ASCENDING = 0;
const bool DESCENDING = 1;

int sortDigit(int number, bool order)
{

    if (order == ASCENDING && number > 0)
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = number; j > 0; j /= 10)
            {
                if (j % 10 == i)
                {
                    cout << i;
                }
                else
                    continue;
            }
        }
    }

    if (order == DESCENDING && number > 0)
    {
        for (int i = 9; i >= 0; i--)
        {
            for (int j = number; j > 0; j /= 10)
            {
                if (j % 10 == i)
                {
                    cout << i;
                }
            }
        }
    }
}

int main()
{
    cout << "sortDigit(54321, ASCENDING) = " << sortDigit(54321, ASCENDING) << endl;
}

The code is supposed to sort numbers in arranging or descending order. Every time I run it, after arranging the numbers in desired order the output is followed by 32767 Ex:- sortDigit(54321, ASCENDING) output:- 1234532767

Your function sortDigit() is already printing out the output. You don't need to send the return value of sortDigit() , declared as an int , to cout too.

This is what you see after the output of sortDigit() . The << operators to cout are processed from left to right. First the initial constant text is printed out. Then sortDigit() is called to get its return value and it prints out some text. Then the return value of sortDigit() is printed. That's the 32767.

You aren't actually returning anything from sortDigit() (where is the return statement?), so you basically get a random value. The proper term is undefined behavior.

Turning on compiler warnings, and paying attention to them, is good idea. They will catch this stuff.

<source>: In function 'int sortDigit(int, bool)':    
<source>:32:1: warning: no return statement in function returning non-void [-Wreturn-type]    
   32 | }    
      | ^

In

cout << "sortDigit(54321, ASCENDING) = " << sortDigit(54321, ASCENDING) << endl;

Your function sortDigit() has no return value, so your code has undefined behaviour, in your case it prints 32767, in mine 0.

12345 is the orderded number which is printed out in your function, followed by the undefined value printed in main() .

If you do just:

int main() {
    sortDigit(54321, ASCENDING);
}

You will have the correct output.

Live sample

If you have that option it would be simpler to use std::sort form the <algorithm> standard library:

int arr[] = { 4,6,9,4,2,1 };

sort(&arr[0], &arr[sizeof(arr)/sizeof(arr[0])]); // ascending

sort(&arr[0], &arr[sizeof(arr)/sizeof(arr[0])], greater<int>()); //descending

Live Sample

Using C++ standard containers like std::vector or std::array is also advised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM