Solved
Simple question (exponents)
Posted on 2003-03-16
Hi! I am new to this forum and C++, so my question will probably be very easy to answer for you: (Please note it is not done yet)
I am translating from TrueBASIC a program which will translate binary to interger. Part of it includes having 2 be to the xth power. I have Visual C++ 5, and it says iy should be in this format:
answer = number^exponent (Exactly as in BASIC)
Now, I have already tried this but I cannot get it to work at all. Here is the code:
#include <iostream.h>
#include <string.h>
int main()
{
int answer, binary, x, number;
x = -1;
answer = 0;
cout << "This program will convert binary numbers into ordinary numbers."<<endl<<endl<<endl<<"What binary number do you want to convert?"<<endl<<"(Please type numbers in one at a time STARTING FROM RIGHT TO LEFT)"<<endl<<"When done, please press any number above 1"<<endl;
cin >> binary;
while (binary < 2)
{
x = x + 1;
if (binary == 0){
cout << "0"<<" ";}
else{
cout << "1"<<" ";
number = 2^x; //here is the problem!
answer = answer + number;}
cout << answer<<endl;
cin >> binary;
}
cout <<endl<<endl<<endl<<"The final answer is: "<<answer<<endl;
return 0;
}
Is there anything I should include? I have already tried math.h, yet it does not solve the problem.