I have a project for c++ and I am having a hard time getting the menu to work. I have tried so many things and I just can't get it!
I would really appreciate any help anyone can give me. Thank you!
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double areaCir;
double radiusCir;
double areaRec;
double lengthRec;
double widthRec;
double areaTri;
double baseTri;
double heightTri;
int userChoice;
const int CIRCLE_CHOICE = 1;
const int RECTANGLE_CHOICE = 2;
const int TRIANGLE_CHOICE = 3;
const int QUIT_CHOICE = 4;
cout << "Hello, welcome to area calculations of basic geometry.";
cout << endl;
cout << "Let's start by choosing your shape.";
cout << endl;
cout << "1. Area of a circle";
cout <<endl;
cout << "2. Area of a rectangle.";
cout <<endl;
cout << "3. Area of a triangle.";
cout << endl;
cout << "4. Quit.";
cout << endl;
cin >> userChoice;
cout <<endl;
if (userChoice == CIRCLE_CHOICE);
{
cout <<"Time to calculate the area of a circle!";
cout <<endl;
cout << "Please enter the radius of your circle.";
cout << endl;
cin >> radiusCir;
areaCir = M_PI*(pow(radiusCir,2)) ;
cout << "The area of your circle is " << areaCir << ".";
cout << endl;
}
if (userChoice == RECTANGLE_CHOICE);
{
cout <<"Now let's calculate the area of a rectangle.";
cout <<endl;
cout << "Please enter the length of your rectangle.";
cout << endl;
cin >> lengthRec;
cout <<"Cool. Now enter the width of your rectangle.";
cout <<endl;
cin >> widthRec;
cout << "The area of your rectange is " << widthRec * lengthRec << ".";
cout << endl;
}
if (userChoice == TRIANGLE_CHOICE);
{
cout <<"We are going to find the area of a triangle!";
cout <<endl;
cout <<"Please enter the base of your triangle.";
cout << endl;
cin >> baseTri;
cout << endl;
cout << "Great! Now enter the height of your triangle.";
cin >> heightTri;
cout << endl;
areaTri = (0.5 * baseTri) * heightTri;
cout << "The area of your triangle is " << areaTri << ".";
cout << endl;
}
if statements do NOT get a ; on them.
; can be a do-nothing statement.
your code says if (whatever == choice) then do nothing.
for all the cases.
a modern compiler would warn you about this.
little stuff..
enum and enum class let you define constant integers in a nice way.
small powers (squared and cubed mostly) are generally better off written x*x than pow(x,2). It does not matter here, but pow is a good bit slower than a multiply or two.
Nice work though, you seem to be having fun with the print statements. That is how so many of us got started... hey look, I can print my name 100000000 times :) Being able to make the box say something got so many 'kids' (loosely speaking, back then that meant anyone under 30) into coding in the early days...
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main() {
int userChoice {};
constint CIRCLE_CHOICE {1};
constint RECTANGLE_CHOICE {2};
constint TRIANGLE_CHOICE {3};
constint QUIT_CHOICE {4};
cout << "Hello, welcome to area calculations of basic geometry.\b";
cout << "Let's start by choosing your shape.\n";
cout << "1. Area of a circle\n";
cout << "2. Area of a rectangle.\n";
cout << "3. Area of a triangle.\n";
cout << "4. Quit.\n";
cout << "Enter option: ";
cin >> userChoice;
if (userChoice == CIRCLE_CHOICE) {
double radiusCir {};
cout << "\nTime to calculate the area of a circle!\n";
cout << "Please enter the radius of your circle: ";
cin >> radiusCir;
cout << "The area of your circle is " << M_PI * radiusCir * radiusCir << ".\n";
}
if (userChoice == RECTANGLE_CHOICE) {
double lengthRec {}, widthRec {};
cout << "\nNow let's calculate the area of a rectangle.\n";
cout << "Please enter the length of your rectangle: ";
cin >> lengthRec;
cout << "Cool. Now enter the width of your rectangle: ";
cin >> widthRec;
cout << "The area of your rectangle is " << widthRec * lengthRec << ".\n";
}
if (userChoice == TRIANGLE_CHOICE) {
double baseTri {}, heightTri {};
cout << "\nWe are going to find the area of a triangle!\n";
cout << "Please enter the base of your triangle: ";
cin >> baseTri;
cout << "Great! Now enter the height of your triangle.";
cin >> heightTri;
cout << "The area of your triangle is " << 0.5 * baseTri * heightTri << ".\n";
}
}