0

im simple asking if this is ok. i was asked to do the following. Write a program that will continuously ask the user for positive integers (until the user enters a negative integer at which the program will be terminated). Every time the user inputs a positive integer the program will print out this integer in reverse. Your program should have a function that accepts and integer and returns an integer in reverse. To convert the integer to its reverse, your program will call this function. at the end of each output i keep getting 0. please explain why. also if i use void main with the function i get garbage. please explain why. thanks in advance

this is my code....

#include<iostream>
#include<cstdlib>

using namespace std;

int reverseNum(int num){
    for(int j=num; j>0; j--)
        cout<<j<<" ";
        cout<<endl;
    return false;
}

int main(){
    double enternum = 0;

    do{
        cout<<"Enter a positive number > 0, to begin countdown ";
        cin >>enternum; 
        cout<<reverseNum(enternum);
        cout<<endl;
    }
    while(enternum>0);

    if(enternum<=0)
        cout<<"Invalid entry, good bye.";
    cout<<endl;

    return 0;
}
Darius Kucinskas
  • 10,193
  • 12
  • 57
  • 79
user836910
  • 484
  • 2
  • 9
  • 22
  • 3
    Is this homework? Also, lots of problems with your code. – crashmstr Sep 12 '11 at 14:39
  • 1
    does it need to be double or int? – sehe Sep 12 '11 at 14:42
  • @crashmstr pls explain these problem – user836910 Sep 12 '11 at 14:44
  • 2
    @user836910: The description seems to indicate that for input `12345` you expect output `54321`, but in your code it would print `12345 12344 12343...` That from the highest level of abstraction, then going to the code, you read a `double` but pass it to a function that takes an `int`, you return a `bool` (always `false`) from a function that is meant to return an `int`... and you print that return value? Always `0`? You should probably start thinking on what the actual problem is, then on the design of a solution (what should your interfaces look like?) and then implement those interfaces. – David Rodríguez - dribeas Sep 12 '11 at 14:50
  • 2
    1. You say you ask for an integer but store it in a double, and then provide that double to a function that takes an integer. 2. it doesn't look like you need to `#include`. 3. [This](http://stackoverflow.com/q/24853/906442) 4. It's unclear whether you don't understand the problem or we don't understand you... – Keith Layne Sep 12 '11 at 14:58
  • Just for fun [I tried coming up with a floating point version](http://stackoverflow.com/questions/7389535/print-number-in-reverse/7390778#7390778). I just realized a flaw in the output scaling, but I'll leave it for now :) – sehe Sep 12 '11 at 16:13

8 Answers8

2

because of this: return false; - I'll leave it to you to figure out the rest..

Nim
  • 33,299
  • 2
  • 62
  • 101
  • i know, use return num and got the number that i entered each time. false return 0, true return 1. mostly used in bool function. because this is not a bool function. im not sure what to return – user836910 Sep 12 '11 at 14:42
  • @user - if there is no need to return anything, then `void...` will suffice - also, there is no need to print the return value (which you won't be able to do if the return type of the function is changed to `void`) - however - I will urge you to re-read the question... – Nim Sep 12 '11 at 14:43
0
             ***//here is the simple solution to find reverse of a function***  
#include<iostream.h>
#include<conio.h>
void main()
{
 int n,a,c,d,b;
clrscr();
cout<<"enter five integers";

 cin>>n;
 a=n/10000;
 n=n%10000;
 b=n/1000;
 n=n%1000;
 c=n/100;
 n=n%100;
 d=n/10;
 n=n%10;
 cout<<"number in reverse order is"<<n<<d<<c<<b<<a;
 getch();
 }
Rippa
  • 1
0

The function is supposed to reverse the integer and then return the result. For example, if the input is 123, then the function returns 321.

Your function outputs a count-down and returns 0 (=false).

To reverse a number, you can a) convert it to string, reverse the string, convert it back to integer; b) do it on integers directly with mathematical division / multiplication / addition / modulo operations.

UncleBens
  • 40,819
  • 6
  • 57
  • 90
0

In C++, you don't use void main(). A 0 because when you return false, the result of type bool is implicitly converted to an int and gets printed at the line cout<<reverseNum(enternum);

Also, In this line, double enternum = 0; you want an integer int.

cpx
  • 17,009
  • 20
  • 87
  • 142
0

From your text I thought the program was working as intended, but from reading the code I suppose it just counts down from the number. Was this what you wanted? I'd have implemented it like this (and here the function returning an integer makes sense too):

int reverseNum(int num)
{
    int reverse = 0;
    [...] // Do the actual reversing
    return reverse;
}
Nornagest
  • 176
  • 5
  • 2
    I'd heartily discourage providing an answer - it's clearly homework, and the OP needs to learn for him(her)self, and one **very important** aspect of learning is learning to read the question... my2c. – Nim Sep 12 '11 at 14:46
  • You're surely right and I took your advise, but I think if he wants to cheat with homeworks he finds a way. Sooner or later he should be forced to think about the code he borrowed from different places if he wants to pass his tests. ;-) – Nornagest Sep 12 '11 at 14:49
  • Every time the user inputs a positive integer the program will print out this integer in reverse. Your program should have a function that accepts and integer and returns an integer in reverse. i will reverse your code – user836910 Sep 12 '11 at 14:50
0

Your program should have a function that accepts and integer and returns an integer in reverse

your reverseNum function should return the reversed integer, not false. and it shouldn't print the number as well, it's the caller which supposed to print it.

if one does:

i = reverseNum(1234);

then i should contain 4321 as an integer (NOT string).

the reason you keep getting 0 is because false is equivalent to 0 as an integer.

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
0

You should read the C++ FAQ in its entirety. You should especially read this. You should also learn how to debug your code. If you stepped through your code in a debugger then all the answers that you have been given here will be obvious.

Keith Layne
  • 3,688
  • 1
  • 24
  • 28
0

For good fun, I attempted a generic implementation that supports any integral or floating point type supported by your compiler.

Be warned, there are a number of issues:

  • reversing a floating point number is not well defined semantically (how to position the decimal separator? How do we handle exponential notation?)
  • floating point types are frequently inexact (at least common IEEE formats are) and hence scaling the input will introduce artificial fractional digits. I have not taken much effort to do proper rounding, so some numbers will reverse into strange things (e.g. 123.0 could reverse into 992.1 instead of 321.0 (untested for this input, try some yourself))
  • the implementation is laughably template-happy. Think of it as the instructional part of this playful answer.

Oh, uncomment the DEBUGTRACE definition to ... get debug tracing :)

See it live here [click]TM

#include <cmath>
#include <limits>
#include <iostream>

#define MAX_DECIMAL_FRACTION 5
#define DEBUGTRACE(x) // do { std::cerr << x; } while (false)

template <typename T, bool is_integer> struct reverse_impl;

template <typename T>
    struct reverse_impl<T, true>
{
    static T reverse(T input)
    {
        T output;

        for (output = 0; input; input/=10)
            output = (output * 10) + input % 10;

        return output;
    }
};

template <typename T>
    struct reverse_impl<T, false>
{
    static T reverse(T input)
    {
        if (std::abs(input) <= std::numeric_limits<T>::epsilon())
            return T(0);

        // scale input
        int log10 = (int) (std::log(input)/std::log(T(10)));
        input *= std::pow(10, MAX_DECIMAL_FRACTION);
        input = std::floor(input);
        input /= std::pow(10, log10+MAX_DECIMAL_FRACTION);

        DEBUGTRACE("debug: scaled " << input << " digits: ");

        int iteration = std::max(log10+MAX_DECIMAL_FRACTION, MAX_DECIMAL_FRACTION);

        if (std::floor(input) < 1)
        {
            input *= 10;
            iteration--;
        }

        T output;
        for (output = T(0); 
             iteration-- && std::floor(input) >= 1; 
             input-=std::floor(input), input*=T(10))
        {
            output = (output / T(10)) + std::floor(input);
            DEBUGTRACE(std::floor(input));
        }

        DEBUGTRACE(std::endl);
        return output * std::pow(10, log10);
    }
};

template <typename T>
    T reverse(T input)
{ 
    return reverse_impl<T, std::numeric_limits<T>::is_integer>::reverse(input); 
}

int main()
{
    std::cout << reverse(-123l) << std::endl;
    std::cout << reverse(123ul) << std::endl;

    std::cout << reverse(123456.0) << std::endl;
    std::cout << reverse(0.027f) << std::endl;

    return 0;
}
sehe
  • 374,641
  • 47
  • 450
  • 633