I'm trying to create classes and put them in a menu. Why isn't this working?
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main(){
int choice;
cout << "Choose numbers from 1-5 to select your option: " << endl;
cout << "[1]: Square" << endl;
cout << "[2]: Rectangle" << endl;
cout << "[3]: Circle" << endl;
cout << "[4]: Traingle" << endl;
cout << "[5]: EXIT" << endl;
cin >> choice;
}
switch(choice)
{
case 1:
class CSquare {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
int per () {return (x*y);}
};
void CSquare::set_values (int a, int b) {
x = a;
y = b;
}
int main4() {
CSquare sq;
int a;
cout << "Enter the lenght of the square: ";
cin >> a;
cout << endl;
sq.set_values (a,2);
cout << "Area Square: " << sq.area() << endl;
sq.set_values (a,4);
cout << "Perimeter Square: " << sq.per() << endl;
return 0;
}
break;
case 2:
class CRectangle{
int f, g;
public:
void set_values (int,int);
int area () {return (f*g);}
int per () {return 2*(f*g);}
};
void CRectangle::set_values (int q, int p) {
f = q;
g = p;
}
int main1() {
CRectangle rect;
int a,b;
cout << "Enter the Lenght of the Rectangle: ";
cin >> a;
cout << "Enter the Width of the Rectangle: ";
cin >> b;
cout << endl;
rect.set_values (a,b);
cout << "Area Rectangle: " << rect.area() << endl;
rect.set_values (a,b);
cout << "Perimeter Rectangle: " << rect.per() << endl;
return 0;
}
break;
case 3:
class CCircle {
float r, d;
public:
void set_values (int,int);
int cir () {return (3.14159*d);}
int diameter () {return (2*r);}
};
void CCircle::set_values (int i, int j) {
r = i;
d = j;
}
int main2() {
CCircle cir;
float a,b;
cout << "Enter the Diameter of the Circle: ";
cin >> a;
cout << "Enter the Radius of the Circle: ";
cin >> b;
cout << endl;
cir.set_values (3.14159,a);
cout << "Circumference Circle: " << cir.cir() << endl;
cir.set_values (2,b);
cout << "Diameter: " << cir.diameter() << endl;
return 0;
}
break;
default:
cout << "END";
break;
}
The indentation is gone so it's a bit hard to see what is going on but you can't have switches outside functions.
The indentation is gone so it's hard to see what is going on but you can't have switches outside of functions.
Both functions and classes are declared/implemented outside of any functions.
Inside a function you create variables and call functions and do the math.
How can i do this please?
Thanks VERY VERY MUCH!! :)