hi everyone. i hope y'all r doin great today. i saw this prob somewhere and tried to solve it but being such a rookie, my nested if statements just wont work. can somebody please try it out and show me how it can be done?
Geometry Calculator:
* Use nested if-else statements for all decision making in the program.
* Declare PI as a double constant with value 3.14159
* Use a while loop to repeat until the user types a 4 as shown below. If the operation code is not 1 through 4, then print an error message and go to the next data item.
* If a negative data value is entered by the user (or read from file), make it positive and use it.
* Use double for all your data variables and print all values with 4 decimal places.
* When the operation is 4, print "Thank you for using my calculator" and end the program.
* In the data given, the first figure is the operation value. An operation value of 1 refers a circle and the next value is the radius. An operation value of 2 or 3 refers to a triangle and the values after it are the base and height of the triangle. Operation values of 5 and 7 should show error messages
int operation;
// display the menu
cin >> operation;
while (operation != 4)
{
// put your calculations here
// display the menu
cin >> operation;
}
After you have the program running on the screen, change the I/O to file I/O. Your output should be as shown in the sample below, with a blank line between each output:
The area of a circle with radius 4.5800 is 14.3885
Error: 5 is not a valid operation
The area of a triangle with base 7.0000 and height 13.0000 is 45.5000
Thank you for using my calculator
stil on the program that cant work. i just began writin the code, putting in more and more and testing it. heres what i've got so far but my answers and not what they shouls be. cld someone check it out and tell me where i'm going wrong. i cant go forward from here
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//declarations
int operation;
double CirRadius, CirArea, Base, Height, TriArea;
const double PI = 3.14159;
// menu
cin >> operation;
while (operation !=4)
{
if (operation == 1)
{
cin >> CirRadius;
CirArea = PI * CirRadius * CirRadius;
cout<<"The area of a circle with radius "<<CirRadius<<" is " <<setprecision(4)<<CirArea<<'\n\n';
cin >> operation;
}
else if (operation == 2 || operation == 3)
{
cin >> Base;
cin >> Height;
TriArea = 0.5 * Base * Height;
cout<<"The area of a triangle with base "<<Base<<" and height "<<Height<<" is "<<TriArea<<'\n\n';
cin >> operation;
}
else if (operation == 5 || operation == 7)
{
cout<<"Error: "<<operation<<" is not a valid operation.\n\n";
It's okay to use \n with std::cout. The real problem is that OP's using '\n\n', and not "\n\n".
Single quotes are used to represent single characters. Double quotes are used to represent a string.
Please edit your post and use code tags. Also indent it properly. Do that and then I will consider taking a look at your program. Also, what is the error? You haven't told us what is happening vs. what you expect. I don't want to have to sort through your homework instructions and debug the program for you. You have to be more specific about what you want help with.
i ran your code with the input you gave in the first post and i got this:
The area of a circle with radius 4.5800 is 65.8992
The are of a rectangle with length 6.3400 and width 5.8000 is 36.7720
The area of a triangle with base 7.0000 and height -13.0000 is -45.5000
Error: 5 is not a valid operation.
The are of a rectangle with length -6.0000 and width 19.4443 is -116.6658
The area of a triangle with base 81.8000 and height 0.5430 is 22.2087
The area of a circle with radius -8976.0000 is 253113432.6758
Error: 7 is not a valid operation.
The are of a rectangle with length 12.5800 and width 3.0000 is 37.7400
Thank you for using my calculator.
i think the output is fine!!! whats the problem??
a couple of places you have put \n in '\n' , change it to either endl or put this in double quotes. rest your logic is perfect and the output is coming nicely.