i 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
I solved your problem, the next program does the trick :)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// geometry.cpp : main project file.
#include "stdafx.h"  // Visual Studio Specific library
#include <iostream>
#include <string>
#include <fstream>
#include <exception>

using namespace std;

const double pi = 3.14159;

class geometry
{
	double width, height, radius;
public:
	geometry();
	~geometry();
	void start(char*);
	void calculate_rectangle();
	void calculate_triangle();
	void calculate_circle();
};

geometry::geometry()
{
	cout.setf(ios::fixed,ios::floatfield);
	cout.precision(4);
	this->width=0;
	this->height=0;
	this->radius=0;
};

geometry::~geometry()
{
	cout<<"Thank you for using my calculator\n";
};

void geometry::start(char* filename)
{
	char * cstring;
	char * piece1 = "";
	char * piece2 = "";
	char * piece3 = "";
	fstream file;

	string instruction;
	char option;
	bool exit=false;
	if(strlen(filename)>0)
	{
		file.open((const char*) filename, ios::beg | ios::in );
		if(!file.is_open())
		{
			throw;
		}
	}

	while(!exit)
	{
		if(strlen(filename)>0)
		{
			if(file.eof()) exit=true;
			getline(file,instruction);
		}
		else
		{
			getline(cin,instruction);
		}
		if(instruction.size()!=0)
		{
			option=instruction.at(0);

			cstring = new char[instruction.size()+1];
			strcpy(cstring,instruction.c_str());

			piece1 = strtok(cstring, " ");
			if(piece1!=NULL);
			piece2 = strtok(NULL, " ");
			if(piece2!=NULL&&(option!='3'||option!='4'));
			piece3 = strtok(NULL, " ");

			if(piece2!=NULL&&option!='4')
			{
				this->width=atof(piece2);
				this->radius=atof(piece2);
				if(piece3!=NULL&&(option!='3'||option!='4'))
				{
					this->height=atof(piece3);
				}
				if(this->width<0.0)  this->width*=-1.0;
				if(this->radius<0.0) this->radius*=-1.0;
				if(piece3!=NULL&&(option!='3'||option!='4'))
				{
					if(this->height<0.0) this->height*=-1.0;
				}
			}
			if(((option=='1'||option=='2')&&piece2!=NULL&&piece3!=NULL)||
				(option=='3'&&piece2!=NULL)||
				(option=='4'))
			{
				switch (option)
				{
					case '1': this->calculate_rectangle();break;
					case '2': this->calculate_triangle();break;
					case '3': this->calculate_circle();break;
					case '4': exit=true;break;	
				}
				this->width=0.0;
				this->height=0.0;
				this->radius=0.0;
			}
			else
			{
				cout<<"error: "<<instruction<<" is not a valid operation\n";
			}
			delete cstring;
		}
	}
	if(file.is_open())
	{
		file.close();
	}
};

void geometry::calculate_rectangle()
{
	cout<<"the area of a rectangle with width "<<this->width
		<<" and height "<<this->height
		<<" is "<<this->width*this->height
		<<endl;
};

void geometry::calculate_triangle()
{
	cout<<"the area of a triangle width base "<<this->width
		<<" and height "<<this->height
		<<" is "<<this->width*this->height/2
		<<endl;
};

void geometry::calculate_circle()
{
	cout<<"the area of a circle with radius "<<this->radius
		<<" is "<<this->radius*this->radius*pi
		<<endl;
};

int main(int argc, char* argv[])
{
	char * filename = "";
	geometry * calculator = new geometry();

	if(argc==2)
	{
		filename=argv[1];
	}
	try
	{
		calculator->start(filename);
	}
	catch(...)
	{
		cout<<"file not found\n";
	}
	delete calculator;
    return 0;
}


With regards, Ronnie van Aarle.
By the way, use it like:

geometry

or

geometry <filename_of_file_with_instructions>
Topic archived. No new replies allowed.