Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// reversenum.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std ;

int main()
{

	int num[100] ;
    int j = 0 , k ;
    int number = 0 ;
    int temp  ;
    int a = 0 , i ;
	int n ;
	cout << "please enter the numbers of numbers that you want :" << endl ;
	cin >> n ;

	cout << "please enter the numbers " << endl ;
	for( int j = 0 ; j < n ; j++ ){
		cin >> num[j] ;

	temp = num[j] ;
   while( temp / 10 > 0 )
	{
	temp /= 10 ;
		a++ ;	
	}
   for( ; num[j] / 10 != 0 ; num[j] /= 10 , a-- )
     {
        k = num[j] % 10 ;
        for( i = 0 ; i < a ; i++ )
          {
             k*=10;
          }

         number=k+number;
		
     }
   
   number += num[j]%10 ;
		
   cout << number <<endl ;

	}
	  
	
	return 0;
}

it cannot clear the first number then put the next number ?
Posted
Updated 10-Apr-11 20:56pm
v3

The problem is that you use the same variable (num) for all the input numbers. Hence you may either
  • Use an array instead of a single variable

or
  • Perform the reversing operation in the same loop you use for getting the input number by the user.


:-)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Apr-11 15:56pm    
Well spotted, correct advice. My 5.
--SA
To be more clear:
for( int i = 0 ; i < n ; i++ )
    cin >>num ;

You are over-writing the inputs here. In num, only the last one remains.

What is being suggested is to use an array instead. Store all the inputs and then try to reverse them.


BTW, it looks like that you got this code to from somewhere that reverses a number. This observation is based on the return type of your reverse() method and the way you use it in Main().

In order to reverse all the numbers that are input and then print it out, you need to make the change to method return type too.
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 14:56pm    
Good work, my 5 :)
Sandeep Mewara 11-Apr-11 0:31am    
Thanks Espen.
Sergey Alexandrovich Kryukov 10-Apr-11 15:55pm    
Valid notes, my 5.
--SA
Sandeep Mewara 11-Apr-11 0:31am    
Thanks SA
Why make it complicated?
C++
#include<stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//----------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    cout << "please don't write plz, and enter the numbers of numbers that you want:" << endl;

    vector<int> values;
    int count;
    cin >> count;

    int value;

    cout << "please don't write plz, and enter the numbers" << endl ;
    for( int i = 0 ; i < count ; i++ )
    {
        cin >> value;
        values.push_back(value);
    }

    for (vector<int>::reverse_iterator it = values.rbegin(); it != values.rend(); it++)
    {
        value = *it;
        cout << value << endl;
    }

    return 0;
}


Regards
Espen Harlinn
 
Share this answer
 
Comments
Albert Holguin 10-Apr-11 15:06pm    
certainly a more elegant solution
Espen Harlinn 10-Apr-11 15:08pm    
Thanks Albert :)
Sergey Alexandrovich Kryukov 10-Apr-11 15:55pm    
No reason to make it more complicated than it has to be. :-) My 5.
--SA
Espen Harlinn 10-Apr-11 16:00pm    
Thanks SAKryukov!
Sandeep Mewara 11-Apr-11 0:32am    
Great work Espen!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900