calculator

Hey guys, making something that asks for input and returns whether it is a power of two or not. It also returns which power of two it is, and thats what im having trouble with. It actually works fine, except that it will always give the number 4683872 between the power and rest of the sentence. eg, "32 is the 5th4683872 power of two!" any ideas why? thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 #include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

void powercheck(int a);
int ender(int i);

int main()
{
int input;
cout<<"Enter a number: \n";
cin>>input;
powercheck(input);
}



void powercheck(int a)
{


for(int i=0;i<=(a/2);i++)
    {

    if (a==(pow(2,i)))
        {
            cout<<a<<" is the ";
            cout<<ender(i);
            cout<<" power of two!"<<endl;
        exit(0);
                }

    }


            cout<<"sorry, "<< a<<" is not a power of two!"<<endl;
}



int ender(int i)
{

    if (i==1)
    {
    cout<<i<<"st";
    }

    else if (i==2)
    {
    cout<<i<<"nd";
    }

    else if (i==3)
    {
    cout<<i<<"rd";
    }

    else if (i>=4)
{
    cout<<i<<"th";
}

}
You are printing the return value of the ender function but you have no return statement within that function.
well how do i return that?
Well, if you want to return the value 5 you just write return 5; but I'm not sure you actually want the ender function to return anything. If you don't want it to return anything you can simply change the return type to void and call the function without cout<< on line 29.
ahh thank you
Topic archived. No new replies allowed.