(solved)cant get this program to work

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;
}


OPERATIONS & DATA VALUES

1 4.58
2 6.34 5.8
3 7 -13
5
2 -6 19.4443
3 81.8 0.543
1 -8976
7
2 12.58 3
4

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
Last edited on
whats the problem then? looks everything is running fine..!!
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";

}

}

return 0;
}


Last edited on
dont use \n with cout, use endl instead.

cout<<"The area of a circle with radius "<<CirRadius<<" is " <<setprecision(4)<<CirArea<< endl;

and check the results. :)
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.
thanks for the hints. somehow i'm stil gettin errors in my output after changing to endl.
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.
whats the last version of your program? can you post it?
yes writetonsharma.
here's what i finally did


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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <fstream>
#include <iomanip>
using namespace std;
 
int main()
{
	int operation;
	const double PI = 3.14159;
	double Tri_Area, Rect_Area, Cir_Area, Tri_Base, Tri_Height, Rect_Width,     Rect_Length, Cir_Radius;

	ifstream infile;
	ofstream outfile;
	
	infile.open ("my_input_file.txt");
	outfile.open ("my_output_file.txt");

        outfile <<fixed<<showpoint<<setprecision(4);
	
	infile >> operation;
	while (operation != 4)
	{
		if (operation == 1)
		{
			infile >> Cir_Radius;
			Cir_Area = PI*Cir_Radius*Cir_Radius;
			outfile <<"The area of a circle with radius "<<Cir_Radius<<" is "<<Cir_Area<<'\n'<<endl;
			infile >> operation;
		}
		else if (operation == 2)
		{
			infile >> Rect_Length >> Rect_Width;
			Rect_Area = Rect_Length * Rect_Width;
			outfile <<"The are of a rectangle with length "<<Rect_Length <<" and width "<<Rect_Width<<" is "<<Rect_Area<<'\n'<<endl;
			infile >> operation;
		}
		else if (operation == 3)
		{
			infile >> Tri_Base >> Tri_Height;
			Tri_Area = (Tri_Base * Tri_Height)/2;
			outfile <<"The area of a triangle with base "<<Tri_Base<<" and height "<<Tri_Height<<" is "<<Tri_Area<<'\n'<<endl;
			infile >> operation;
		}
		else 
		{
			outfile <<"Error: "<<operation<<" is not a valid operation.\n\n";
			infile >> operation;
		}
	}
	if (operation == 4)
	{
		outfile <<"Thank you for using my calculator."<< endl;
	}


	return 0;

}



stil learning so i dont yet know all the shortcuts to do all the stuff
Last edited on
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.
a couple of places you have put \n in '\n' , change it to either endl or put this in double quotes.
Why should he do this?
Topic archived. No new replies allowed.