DO LOOP Problem

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.


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
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace 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;
}
Last edited on
What errors are you getting, if any, or is it just skipping that part of the code.
I am getting

"error: 'retry' was not declared in this scope."
The error is at line 23.

And in line 20 I labeled it as char


@R23MJ
Last edited on
Ah, move the declaration of retry out of the do loop, just above it should work.
@R23MJ
My program works!!! Thanks a lot m8.

So the problem with the program was that i should declare everything on the top before i continue on.

Is their any advice u can give me to help me out?
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.
thanks a ton @R23MJ
Topic archived. No new replies allowed.