#include <iostream>
//multiple feature calculator
using namespace std;
int main( )
{
float a =0;
float b =0;
int c=0;
int result =1;
char q;
char r='w' ;
cout << "Welcome " << endl;
cout << "This calculator uses 2 d.p. (ints only for^) */+-^ only, press enter 2 calculate" << endl;
loop:
cin >> a >> q >> b ;
cout << "=" ;
if (q == '+')
{
cout << a+b ;
goto rpoet ;
}
if (q == '-')
{
cout << a-b ;
goto rpoet ;
}
if (q == '/')
{
cout << a/b ;
goto rpoet ;
}
if (q == '*')
{
cout << a*b ;
goto rpoet ;
}
if (q == '^')
{ if (b <= 0)
{
b=b*-1;
res1:
c=++c ;
result = result*a ;
if (c==b)
{
cout << 1/result ;
goto rpoet;
}
goto res1; }
if (b == 0)
{
cout << "1" ;
goto rpoet ;
}
if (b == 1)
{
cout << a ;
goto rpoet ;
}
if (b >= 1)
{
res:
result = result*a ;
c=++c ;
if (c == b)
{
cout << result ;
goto rpoet;
}
if (c <= b)
{
goto res;
}
cout << endl << "Press X to Exit, Any other key to Repeat" << endl ;
cin >> r ;
if( r == 'X' )
{
cout << "Goodbye" << endl;
goto end ;
}
if (r =='x' )
{
cout << "Goodbye" << endl;
goto end ;
}
{
cout << "Good Choice" << endl;
goto loop ;
}
end:
return 0;
}
this is my program but it wont work with the negative powers. it is either displaying 0 or nothing!
1. learn to format for readability by others and use the code tags. It'll make your code more readable.
2. You don't need to be using goto. Learn how to use loops.
3. You don't re-initialize variables if the user chooses to repeat, so you're going to get incorrect results in subsequent iterations of the program.
Now, as far as the problem you're referring to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (q == '^')
{
if (b <= 0)
{
b=b*-1;
res1:
c=++c ;
result = result*a ;
if (c==b)
{
cout << 1/result ;
goto rpoet;
}
in the line if (c == b) you are comparing an integer to a float. You're fortunate that b happens to be a whole number when it gets to that point. If it were a fractional/rational/irrational, you'd see another problem.
cout << 1/result;
Is doing integer math so the result is going to be an integer. Since integers can't represent floating-point decimal numbers, the output is naturally 0.
You have a lot of issues you need to work on. Make it 1.0/result and you should see a proper output.
thank you, b would be a interger at the time in any resonable circumstance as i dont except people to do fractional powers on it but b needs to be a float for other operations
#include <iostream>
//multiple feature calculator
usingnamespace std;
int main( )
{
float a = 3.5;
int b = 2;
int c=0;
int result =1;
char q;
cout << a + b << endl;
cin.get();
}
Correctly prints 5.5 with a as a float and b as an integer. If one of the terms of a math operation is a fp value, the result is cast/upgraded to an fp number, unless stored in an integer.
you'd be comparing 3.2 to 3 or 4 (c being an integer) if someone typed in a decimal for the power, which would test false every time, because 3.2 != 3 && 3.2 != 4.