I am doing a problem from my book I think I have everything else right
except I can't seem to figure out how the validate choices. Help would be greatly appreciated.
(directions from the book ) Input validation: Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Do no accept negative values for the circle radius, the rectangle's length or width, or the triangle's base or height.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
usingnamespace std;
int main()
{int choice ;// To hold menu choices
constint choiceA = 1,// Constant choic3 value answers 1-4
choiceB = 2,
choiceC = 3,
choiceD = 4;
// dislay the directions and intent of the program
cout << " This program is a simple geometry calculator that will compute\n";
cout << " the area of circle, rectangle, and triangle.\n";
cout << " please select on of the following choices 1-4\n";
cout << " any other other inputs besides 1-4 will cause the program to stop.\n\n";
cout << " 1. To calculate Area of circle:\n";// The menu print out the choices
cout << " 2. To calculate Area of rectangle:\n";
cout << " 3. To calculate Area of triangle:\n";
cout << " 4. To quit:\n\n\n";
cout << " What is your choice ?\n\n";
cin >> choice;
if (choice == choiceA)// The if / else for cirlce area
{
constdouble PI = 3.14159;// const for pi
double circleArea;// variable to hold cirlce area equation
double radius;// user input variable
cout << " Enter the radius :\n";
cin >> radius;
circleArea = PI * pow(radius, 2.0);// to do radius to the snd power this is needed
cout << " The area of a circle is " << setprecision(5) << circleArea << "." << endl;
}
elseif (choice == choiceB)// Display the output and choices #2
{
double lenght;//Variables for the lenght, width, and output
double width;
double recOutput;
cout << " Enter the lenght of rectangle: ";
cin >> lenght;
cout << " Enter the width of rectangle: ";
cin >> width;
recOutput = lenght * width;
cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";
}
elseif ( choice == choiceC)//For the triangle
{
double triBase;//Variables for triangle base, height, output
double triHeight;
double triOutput;
constdouble triangleopp = .5;// constant variable to hole opperand .5
cout << " Enter the Base: ";
cin >> triBase;
cout << " Enter the Height: ";
cin >> triHeight;
triOutput = triangleopp * triBase * triHeight;
cout << "The area of the triangle is " << setprecision(7) << showpoint << triOutput << ".\n";
}
elseif (choice == choiceD)// the quit option
{
string optionQuit;
cout << " Do you wan to quit the program? ";
cin >> optionQuit;
cout << " The program will now quit\n";
}
}
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
//using namespace std; // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
// <--- A better option.
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::showpoint;
using std::setprecision;
using std::string;
int main()
{
int choice;// To hold menu choices
constint choiceA = 1,// Constant choic3 value answers 1-4
choiceB = 2,
choiceC = 3,
choiceD = 4;
// dislay the directions and intent of the program
cout << " This program is a simple geometry calculator that will compute\n";
cout << " the area of circle, rectangle, and triangle.\n";
cout << " please select on of the following choices 1-4\n";
cout << " any other other inputs besides 1-4 will cause the program to stop.\n\n";
// **********************************************************
// NEEED TO VALIDATE INPTU.
cout << " 1. To calculate Area of circle:\n";// The menu print out the choices
cout << " 2. To calculate Area of rectangle:\n";
cout << " 3. To calculate Area of triangle:\n";
cout << " 4. To quit:\n"; // <--- Changed.
cout << " What is your choice ? "; // <--- Changed.
cin >> choice;
// Maybe a do/while loop.
// **********************************************************
if (choice == choiceA)// The if / else for cirlce area
{
constdouble PI = 3.14159;// const for pi
double circleArea;// variable to hold cirlce area equation
double radius;// user input variable
cout << " Enter the radius :\n";
cin >> radius;
// **********************************************************
// NEEED TO VALIDATE INPTU.
// **********************************************************
circleArea = PI * pow(radius, 2.0);// to do radius to the snd power this is needed
cout << " The area of a circle is " << setprecision(5) << circleArea << "." << endl;
}
elseif (choice == choiceB)// Display the output and choices #2
{
double lenght;//Variables for the lenght, width, and output
double width;
double recOutput;
cout << " Enter the lenght of rectangle: ";
cin >> lenght;
// **********************************************************
// NEEED TO VALIDATE INPTU.
// **********************************************************
cout << " Enter the width of rectangle: ";
cin >> width;
// **********************************************************
// NEEED TO VALIDATE INPTU.
// **********************************************************
recOutput = lenght * width;
cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";
}
elseif (choice == choiceC)//For the triangle
{
constdouble triangleopp = .5;// constant variable to hole opperand .5
double triBase;//Variables for triangle base, height, output
double triHeight;
double triOutput;
cout << " Enter the Base: ";
cin >> triBase;
// **********************************************************
// NEEED TO VALIDATE INPTU.
// **********************************************************
cout << " Enter the Height: ";
cin >> triHeight;
triOutput = triangleopp * triBase * triHeight;
cout << "The area of the triangle is " << setprecision(7) << showpoint << triOutput << ".\n";
}
elseif (choice == choiceD)// the quit option
{
string optionQuit;
cout << " Do you wan to quit the program? ";
cin >> optionQuit;
cout << " The program will now quit\n";
}
}
What you would do for lines 38 - 43 would need to be done for the other inputs. If you are having a problem with this just say so.
Andy
Edit:
P.S. watch your indenting and use of {}s. Be consistent with their use.
int main()
{
int choice;// To hold menu choices
constint // Constant choic3 value answers 1-4
choiceA = 1,
choiceB = 2,
choiceC = 3,
choiceD = 4;
// dislay the directions and intent of the program
cout // <--- An alternative way of writing this.
<< " This program is a simple geometry calculator that will compute\n"
<< " the area of circle, rectangle, and triangle.\n"
<< " please select on of the following choices 1-4\n" << " any other other"
<< "inputs besides 1-4 will cause the program to stop.\n" // <--- 2 "\n"s should work and flush the output buffer.
<< std::endl; // <--- Or save the "endl" for after the last line.
// **********************************************************
// NEED TO VALIDATE INPUT.
do
{
cout << " 1. To calculate Area of circle:\n";// The menu print out the choices
cout << " 2. To calculate Area of rectangle:\n";
cout << " 3. To calculate Area of triangle:\n";
cout << " 4. To quit:\n"; // <--- Changed.
cout << " What is your choice ? "; // <--- Changed.
cin >> choice;
if(!cin || choice < 1 || choice > 4)
{
if (!cin)
{
std::cout << "\n Invalid input! Your choice must be a number between (1 and 4)\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
elseif (choice < 1 || choice > 4)
{
std::cout << "\n Invalid choice! Your choice must be a number between (1 and 4)\n\n";
}
}
} while (choice < 1 || choice > 4);
// Maybe a do/while loop.
// **********************************************************