Can you give me some hints as to what I'm doing wrong with this, I'm in a bit of a rush otherwise I could take more time to figure out the errors, it's probably not that hard....sorry for all the whitespace and whatnot. I hope you can follow my code...I think the problem has something to do with variable declaration and brackets syntax because I believe the math computations are correct.
Psuedocode:
User inputs char ‘C’ for circle circumference, ‘D’ for diameter, or ‘A’ for area and these values will be floating-point numbers.
The program should print the input data.
Validate the input by making sure that the first character is ‘A’ or ‘C’ or ‘D’ and the value of the radius is greater than zero. An appropriate error message should be printed if invalid data was entered.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
float radius, circumference, diameter, area, pi;
char letter_input;
//Declare the variables used
pi = 3.14159265;
radius = 0;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(8);
cout << "Would you like to find the circumference, diameter, or area of a circle? Enter C for circumference, D for diameter, or A for area" << endl;
cin >> letter_input;
cout << "You want to find the" << letter_input << "of a circle""Now enter a radius for your circle" << endl;
cin >> radius;
while (radius > 0)//while loop to get radius greater than 0
{
if
{
(letter_input == 'C')
circumference = 2 * pi * radius;
cout << "The circumference of a circle with radius" << radius << "is" << C << endl;
}
elseif
{
(letter_input == 'D')
diameter = 2 * radius;
cout << "The diameter of a circle with radius" << radius << "is" << D << endl;
}
elseif
{
(letter_input == 'A')
area = pi * pow(radius, 2);
cout << "The area of a circle with radius" << radius << "is" << A << endl;
}
else
{
//if radius is less than or equal to 0 prompt re-entry
cout << "Re-enter a value for the radius that is greater than 0" << endl;
cin >> radius;
}
return 0;
}
}
On line 24 you do enter the loop only if you already have a valid radius. If radius is not >0, then the program is over.
If you do enter the loop, and you did have a valid letter, then that calculation occurs repeatedly, because neither letter nor radius can change. Edit: Not repeatedly, for you return, i.e. quit program from within the loop.
If you do enter the loop, but letter was invalid, then you can only change the radius, until it becomes invalid.
Last a note about syntax (that the compiler should have told you already):
The syntax of if is:
1 2 3 4
if ( condition )
{
statements
}
You don't have the condition in its rightful place.