Hello, this program is supposed to receive a numbers from the user to figure out a fast way to solve exponential equations.
i.e. 2^3=8
What is wrong with my do loop? I am trying to ask the user if they would like to retry, the program before it closes out. But I was hoping you can tell me how to do it correctly.
#include <iostream>
#include <conio.h>
#include <cmath>
usingnamespace std;
int main()
{
do
{cout<<"Enter number."<<endl;
int number=0;
cin>>number;
cout<<"Enter exponent."<<endl;
int exponent=0;
cin>>exponent;
double answer= pow(number,exponent);
cout<<"Your answer is "<<answer<<endl;
cout<<"If you would like to retry, then press 'Y'/'y' if not press anything else."<<endl;
char retry;
cin>>retry;
}
while(retry='Y'||'y');
getch();
return 0;
}
Not necessarily everything needs to be declared out of the loop, just what you intend to use outside of the loop. Also, declaring global variables (Ones outside the main function) is generally frowned upon.
As far as tips:
Indent your code a bit, it can clean up readability pretty quickly. Here's an example:
1 2 3 4 5 6 7 8
int main()
{
bool someBool = true;
if (someBool)
std::cout << "Hello!";
return 0;
}
Or something, just to make it visible where if-statements and the like end.
something else to note: Getch() is not standard. I assume you are pausing the program with this? There are other methods, and if I remember correctly, this is usually frowned upon, something about wasting CPU cycles. Don't quote me on the CPU cycles thing, not sure how true it is.
Anywho, a topic on advice would be endless. So I'll leave you there.