Problem with function

When i run the program through all of the formulas work but if i dont hit the right number to goto the menu it says not an option and returns to the choose screen but the second time through it doesnt work also when i hit 5 it doesnt exit the program it asks do u want to to it again. Can anybody help me with these errors. heres my code so far...




#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <math.h>
#include <string>
using namespace std;
float area, base, height, radious, base_1, base_2 ;
int formula;
char again;
string units;
int Main()
{



cout<<setw(50)<<"Choose The Formula For You:\n";
cout<<setw(43)<<"[1]Rectangle\n";
cout<<setw(40)<<"[2]Circle\n";
cout<<setw(42)<<"[3]Triangle\n";
cout<<setw(43)<<"[4]Trapazoid\n";
cout<<setw(38)<<"[5]Exit\n";
cin>>formula;
cin.clear();
}


int option()
{

switch (formula)
{

case 1:

cout<<setw(52)<<"What are your units:";
cin>>units;
cout<<"\n\n";
cout<<setw(48)<<"Enter your base:";
cin>>base;
cout<<"\n\n"<<setw(50)<<"Enter your height:";
cin>>height;
area=base*height;
cout<<"\n\n"<<setw(62)<<"The area of your rectangle is:"<<area<<" "<<units<<" Squared\n";
Sleep(5000);
system("cls");
break;

case 2:

cout<<"\n\n\n";
cout<<setw(49)<<"What are your units:";
cin>>units;
cout<<"\n\n\n";
cout<<setw(45)<<"Enter your base:";
cin>>base;
cout<<"\n\n\n";
cout<<setw(47)<<"Enter your height:";
cin>>height;
area=(.5)*(base*height);
cout<<"\n\n\n";
cout<<setw(58)<<"The area of your triangle is:"<<area<<" "<<units<<" Squared\n";
Sleep(5000);
system("cls");
break;
case 4:

cout<<setw(52)<<"What are your units:";
cin>>units;
cout<<"\n\n";
cout<<setw(45)<<"Enter base 1:";
cin>>base_1;
cout<<"\n\n";
cout<<setw(45)<<"Enter base 2:";
cin>>base_2;
cout<<"\n\n";
cout<<setw(50)<<"Enter your height:";
cin>>height;
cout<<"\n\n";
area=(.5*height)*(base_1+base_2);
cout<<setw(62)<<"The area of your rectangle is:"<<area<<" "<<units<<" Squared\n";;
Sleep(5000);
system("cls");
break;
case 5:
return 0;
break;

default:
cout<<"NOT AN OPTION";
Sleep(1000);
Main();
break;

}

}
int main(int argc, char *argv[])
{
do
{

cin.clear();
Main();
option();
again:
cout<<"would you like to do this again [y] or [n]?";
cin>>again;



}while (again=='y' || again=='Y');
switch (again)
{
case 'n':
return 0;
default:
return 0;
break;
}
}
That works, i just didn't add that repeat thing at the end. Hope I helped.

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
// area of shape.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace System;
using namespace std;

class Area_Finder
{
	double height, base1, base2, radius;
public:
	void set_dim(double &, double &, double &, double &);
	double Calc_Rect();
	double Calc_Tri();
	double Calc_Trap();
	double Calc_Cir();
};

void Area_Finder::set_dim(double &h, double &b1, double &b2, double &r)
{
	height = h;
	base1 = b1;
	base2 = b2;
	radius = r;
}

double Area_Finder::Calc_Rect()
{
	return height * base1;
}

double Area_Finder::Calc_Tri()
{
	return (.5) * (height * base1);
}

double Area_Finder::Calc_Trap()
{
	return (height * (base1 + base2)) / 2;
}

double Area_Finder::Calc_Cir()
{
	return (3.14159265 * radius * radius);
}


int main()
{
	cout << "[1] Rectangle\n";
	cout << "[2] Triangle\n";
	cout << "[3] Trapezoid\n";
	cout << "[4] Circle\n";
	cout << "[5] Exit\n\n";

	cout << "What is the shape you would like to find the Area of? ";

	int a1;
	cin >> a1;
	while(a1 < 1 || a1 > 5) {
		cout << "\nError, not a valid answer, try again.\n";
		cin >> a1;
	}

	Area_Finder shape;
	double h, b1, b2, r;
	switch(a1)
	{
	case 1:
		{
			cout << "\n\nHeight? ";
			cin >> h;

			cout << "\n\nBase? ";
			cin >> b1;

			shape.set_dim(h,b1,b2,r);
			cout << "\n\nArea of Rectangle: " << shape.Calc_Rect() << "\n\n";
			break;
		}
	case 2:
		{
			cout << "\n\nHeight? ";
			cin >> h;

			cout << "\n\nBase? ";
			cin >> b1;

			shape.set_dim(h,b1,b2,r);
			cout << "\n\nArea of Triangle: " << shape.Calc_Tri() << "\n\n";
			break;
		}
	case 3:
		{
			cout << "\n\nHeight? ";
			cin >> h;

			cout << "\n\nBase1? ";
			cin >> b1;

			cout << "\n\nBase2? ";
			cin >> b2;

			shape.set_dim(h,b1,b2,r);
			cout << "\n\nArea of Trapezoid: " << shape.Calc_Trap() << "\n\n";
			break;
		}
	case 4:
		{
			cout << "\n\nRadius? ";
			cin >> r;

			shape.set_dim(h,b1,b2,r);
			cout << "\n\nArea of Circle: " << shape.Calc_Cir() << "\n\n";
			break;
		}
	case 5:
		{
			return 0;
			break;
		}
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.