Maybe someone can help me with this.

So i just made a script, that ofers you to calculate either capacity or field of a ball and also i added a loop, so it offers you to calculate again. The only problem is when i choose to calculate again it does not allow me to choose either a field or capacity, but only the one i choosed already, so if the first time i choosed the capacity, then the second time i will able to calculate only the capacity. If anyone can figure this out i would be very happy.!

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <math.h>
#define PI 3.14159265

using namespace std;

bool Repeat = true;
bool Capacity = true;
bool Field = true;

int main()
{   
    char symbol;
    int R;
    while (Repeat) {
    cout << " What do you want to calculate? " << endl;
    cout << "Field or Capacity of a ball(F or C)?";
    cin >> symbol;
    Capacity = (symbol == 'C' || symbol == 'c') ? true : false;
    Field = (symbol == 'F' || symbol == 'f' ) ? true : false;

    while (Capacity) {
          cout << "You choosed to calculate the capacity" << endl;
          cout << "Enter the radiuss of the ball: ";
          cin >> R;
          cout << "The capacity of a ball is: " << (4*(pow(R,3))*PI/3)<<"cm^3 or " <<
          (4*(pow(R,3))/3) <<"PI cm^3" << endl;
          cout << "Would you like to calculate again?(Y or N): ";
          cin >> symbol;
          Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;
          }
    while (Field) {
          cout << "You choosed to calculate the field" << endl;
          cout << "Enter the radiuss of the ball: ";
          cin >> R;
          cout << "The Field of a ball is: " << (4 * (pow(R,2)) * PI) <<"cm^2 or " <<
          (4*(pow(R,2))) <<"PI cm^2" <<endl;
          cout << "Would you like to calculate again? (Y or N): ";
          cin >> symbol;
          Repeat = (symbol =='Y' || symbol == 'y') ? true : false;
          }
    }

}
In your while (Capacity/Field) loops, you never actually change the bool's Field or Capacity, causing those loops to run forever.
I'm realy a bigginer with about 3 weeks experience in programming, so could you explain more about this and does it mean that i should use for, not while? I have read that with for you get more control with looping.
1
2
3
4
5
6
7
8
9
10
while (Capacity) {
          cout << "You choosed to calculate the capacity" << endl;
          cout << "Enter the radiuss of the ball: ";
          cin >> R;
          cout << "The capacity of a ball is: " << (4*(pow(R,3))*PI/3)<<"cm^3 or " <<
          (4*(pow(R,3))/3) <<"PI cm^3" << endl;
          cout << "Would you like to calculate again?(Y or N): ";
          cin >> symbol;
          Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;
          }


The "Capacity" variable never actually has its value changed, so that loops never ends. You must set it to false at the end of the loop if the user chose not to repeat. Same thing for the Field loop.
Ok i understood the problem, but i don't know how to set it to false at the end of the loop. I tried adding if (Repeat = false) bool Capacity = false , but didn't get any change by doing that.
If you added "bool" before the variable name, then you actually created a new variable, and never changed the old one. This is legal because the scope of the loop is different than the global scope in which the original bool's were declared.
Heh, i must be a retard or something, but i'm just to unexperienced to still understand how to fix this.
Topic archived. No new replies allowed.