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.!
#include <iostream>
#include <math.h>
#define PI 3.14159265
usingnamespace 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;
}
}
}
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.
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.