i wanna show a descending order of a variable not a array element..

i wanna show a descending order of a variable not a array element..like i wanna input 523 that shows me output like 325,,,,,,can anyone help me...
I can help you, but first help me see what you have tried / done / thought
Last edited on
Maybe if someone will understand what you wrote then he help you.:)
@Santosh Reddy
I can help you, but first help me see what you have tried / done / thought


I think he is speaking about outputing an integer number in the reverse order of its digits.:)
Last edited on
well you could do like an array/list/vector/stack that stores every digit. so when you input "523" as a string you can cut it up to "5", "2" and "3".

something like

stack<int> intStack;

then u pass into a function by refernce

1
2
3
4
5
6
7
8
9
void tokenise (string str, stack<int> &intStack) {
	stringstream token(str);     //a standard stringstream which parses 'str'
	string temp;                 //a temporary string

	while (token >> temp){    // while the string has not been completely copied
		int number = convertToInt(checking); // assign "number" a value by converting "checking" into an int
		intStack.push(number); // store "number" into the stack
	}
};
sorry i meant

1
2
3
4
5
6
7
8
9
10
void tokenise (string str, stack<int> &intStack) {
	stringstream token(str);     //a standard stringstream which parses 'str'
	string temp;                 //a temporary string
        int number

	while (token >> temp){    // while the string has not been completely copied
		number = convertToInt(temp); // assign "number" a value by converting "checking" into an int
		intStack.push(number); // store "number" into the stack
	}
};


"checking" in int number = convertToInt(checking); refers to the string in which user has typed
1
2
3
4
5
6
void print_reverse( unsigned int n )
{
    std::cout << n%10 ;
    if( n < 10 ) std::cout << '\n' ;
    else print_reverse( n/10 ) ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <functional>


int main()
{
	std::function<void( unsigned int )> reverse = [&]( unsigned int x ) 
	{ 
		std::cout << x % 10;
		( x /= 10 ) ? reverse( x ) : void( std::cout << std::endl );
	};


	while ( unsigned int x = ( std::cout << "\nEnter a non-negative number (0 - exit): ",
	                           std::cin >> x, x ) )
	{
		reverse( x );
	}

	return 0;
}


EDIT: The code was updated according to the remark of JLBorges
Last edited on
1
2
3
4
5
6
std::function<void( unsigned int )> reverse = [&]( unsigned int x ) 
{ 
    std::cout << x % 10;
    // *** ( x /= 10 ) ? reverse( x ) : std::cout << std::endl;
    ( x /= 10 ) ? void( reverse( x ) ) : void( std::cout << std::endl ) ;
};
@JLBorges


I think that there is no need to casting the expressions to void.

In old C code I saw such constructions

( void )func();

But it is better to write simply

func();

EDIT: Ah, I am sorry. I think that if one of the expressions is of type void then the other expression in the conditional operator can be of any type.

Take into account that the call of reverse already has type void.
Last edited on

@JLBorges


It seems that you are right. The third operand shall be casted to void. According to the C++ Standard

2 If either the second or the third operand has type void, then the lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the second and third operands, and one of the following shall hold:
— The second or the third operand (but not both) is a throw-expression (15.1); the result is of the type of the other and is a prvalue.
— Both the second and the third operands have type void; the result is of type void and is a prvalue.
[ Note: This includes the case where both operands are throw-expressions. —end note ]


So the expression shall be written as

( x /= 10 ) ? reverse( x ) : void( std::cout << std::endl ) ;

Thanks.
Last edited on
> if one of the expressions is of type void then
> the other expression in the conditional operator can be of any type.

No.

It can either be an expression of of type void, or it can be a throw-expression.
http://liveworkspace.org/code/2WBB3q$2
Last edited on
Thanks JLBorges. I have already realized this.
Topic archived. No new replies allowed.