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
#include <iostream>
#include <cstdlib>
#include <cmath>
usingnamespace 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";
}
elseif (i==2)
{
cout<<i<<"nd";
}
elseif (i==3)
{
cout<<i<<"rd";
}
elseif (i>=4)
{
cout<<i<<"th";
}
}
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.