I am trying to use a validation block by via a function because it will be repeated. When I just use the while loop it works but I know I should be able to do a function call also to streamline but it doesn't work.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
usingnamespace std;
int whilevalidate()
{
int userChoice = 0;
while (userChoice < 0) // validation for radius)
{
cout << "Enter postive values please: ";
cin >> userChoice;
}
return userChoice;
}
void geoCalculator()
{
/*cout << "Hello from the other side\n\n";
cout << " I've must have called a thousand times.\n\n";*/
int option = '0';
int choice = 'n';// To hold menu choices
bool cal_continue = true; // for the quit part
//bool radius_val = false; // validation for radius
/*while (radius < 0) // validation for radius)
{
cout << "Enter postive values please:";
cin >> radius;
}*/
do {
cout << " This program is a simple geometry calculator that will compute\n\n"; // dislay the directions and intent of the program
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 << ": 1 - 4 --> \n";
cin >> option;
if (option >= 1 && option <= 4)// The if for menu choices
{
if (option == 1)//Theif for the circle block
{
constdouble PI = 3.14159;// const for pi
longdouble circleArea;// variable to hold cirlce area equation
longdouble radius;// user input variable
cout << " Enter the radius :\n";// Tells the suer to enter radius of circle
cin >> radius;//user enters radius
whilevalidate();
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 (option == 2) // Display the output and choices #2 Rectangle
{
longdouble length;//Variables for the lenght, width, and output
longdouble width;
longdouble recOutput;
cout << " Enter the length of rectangle: ";
cin >> length;
whilevalidate();
cout << " Enter the width of rectangle: ";
cin >> width;
whilevalidate();
recOutput = length * width;
cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";
}
elseif (option == 3) //For the triangle
{
longdouble triBase;//Variables for triangle base, height, output
longdouble triHeight;
longdouble triOutput;
constdouble triangleopp = .5;// constant variable to hole opperand .5
cout << " Enter the Base: ";
cin >> triBase;
whilevalidate();
cout << " Enter the Height: ";
cin >> triHeight;
whilevalidate();
triOutput = (triBase * triHeight) / triangleopp;
cout << "The area of the triangle is " << setprecision(5) << showpoint << triOutput << ".\n";
}
else
{
cout << "exit\n\n";
cal_continue = false;
}
}
} while (cal_continue);
}
int main()
{
geoCalculator();
}
From the book .Input validation;Display an error message if the user enters a number outside the range of 1 - 4 when selecting an item form the menu. Do not accept negative values of for the circle's radius, the rectangle L x W, or the triangle's base or height.
I made the while loop that way because when i did it as a standalone it worked so i just transferred the same to a a function.
I have the return there because its return the value(userChoice) to its call
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
usingnamespace std;
// validation for radius
double whilevalidate(double numberTovalidate)
{
//double userChoice = 0.0 ;
while (numberTovalidate < 0.0) // validation for radius)
{
cout << "Enter postive values please: ";
cin >> (numberTovalidate);
}
return numberTovalidate;
}//validation function for input values
// function to call the menu
void showMenu()
{
cout << " This program is a simple geometry calculator that will compute\n\n" // dislay the directions and intent of the program
<< " 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\n"
<< " 1. To calculate Area of circle:\n"// The menu print out the choices
<< " 2. To calculate Area of rectangle:\n"
<< " 3. To calculate Area of triangle:\n"
<< " 4. To quit:\n\n\n"
<< ": 1 - 4 --> \n";
}
// geo calculator function
void geoCalculator()
{
int option = '0';
int choice = 'n';// To hold menu choices
bool cal_continue = true; // for the quit part
//bool radius_val = false; // validation for radius
/*while (radius < 0) // validation for radius)
{
cout << "Enter postive values please:";
cin >> radius;
}*/
do {
showMenu();
/*cout << " This program is a simple geometry calculator that will compute\n\n"; // dislay the directions and intent of the program
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 << ": 1 - 4 --> \n";*/
cin >> option;
if (option >= 1 && option <= 4)// The if for menu choices
{
if (option == 1)//Theif for the circle block
{
constdouble PI = 3.14159;// const for pi
longdouble circleArea;// variable to hold cirlce area equation
longdouble radius;// user input variable
cout << " Enter the radius :\n";// Tells the suer to enter radius of circle
cin >> radius;//user enters radius
//whilevalidate();
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 (option == 2) // Display the output and choices #2 Rectangle
{
longdouble length;//Variables for the lenght, width, and output
longdouble width;
longdouble recOutput;
cout << " Enter the length of rectangle: ";
cin >> length;
//whilevalidate();
cout << " Enter the width of rectangle: ";
cin >> width;
//whilevalidate();
recOutput = length * width;
cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";
}
elseif (option == 3) //For the triangle
{
longdouble triBase;//Variables for triangle base, height, output
longdouble triHeight;
longdouble triOutput;
constdouble triangleopp = .5;// constant variable to hole opperand .5
cout << " Enter the Base: ";
cin >> triBase;
//whilevalidate();
cout << " Enter the Height: ";
cin >> triHeight;
//whilevalidate();
triOutput = (triBase * triHeight) / triangleopp;
cout << "The area of the triangle is " << setprecision(5) << showpoint << triOutput << ".\n";
}
else
{
cout << "exit\n\n";
cal_continue = false;
}
}
} while (cal_continue);
}
int main()
{
geoCalculator();
}
How does it run just fine for you? When I run and I input a negative value the program compiles it. It should hit my while loop (the validation) however, it does not. I have a long double just because I wanted the range of it.
I have a long double just because I wanted the range of it.
What? Do you know that the "range" of a long double is an implementation detail, some compilers may have the same range for double and long double. For the application you're doing won't really gain much from the use of long double.
It should hit my while loop (the validation) however, it does not.
This is probably because you're either doing something wrong, or you still have the call to the function commented out. By wrong, I mean that your either not returning a value that you actually use from the function or you're not passing any parameters into the function or both.
Note there are several ways of accomplishing the validation, I'll only show one way.
Again this is the function I'm using. I'm using the function this way because it requires the fewest changes to your existing code:
1 2 3 4 5 6 7 8 9 10 11 12
// validation for radius
double whilevalidate(double numberTovalidate)
{
while (numberTovalidate < 0.0)
{
cout << "Enter postive values please: ";
cin >> (numberTovalidate);
}
return numberTovalidate;
}//validation function for input values
Now this is the part of main() that is using the function:
1 2 3 4 5 6 7 8 9 10 11 12
if (option == 1)//Theif for the circle block
{
constdouble PI = 3.14159;// const for pi
double circleArea;// variable to hold cirlce area equation
double radius = -1;// user input variable
cout << " Enter the radius :\n";// Tells the suer to enter radius of circle
cin >> radius;//user enters radius
radius = whilevalidate(radius);
circleArea = PI * radius * radius; //pow(radius, 2.0);// to do radius to the snd power this is needed
cout << "The area of a circle is " << setprecision(5) << circleArea << ".\n\n" << endl;
}
So the while function that's in the first if statement is the only one that changed. Disregard the other statements. Once I figure out how to function call correctly I will use the function call validation in place of the while loop.
If you run the code and select option 1 and when it asks for you to enter the radius input a negative number and it.
EDIT( This is written with the while loop directly inserted in the statement. Again My problem is when I try to use the function from within a function and be called to the geo calculator function.)
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
usingnamespace std;
void geoCalculator()
{
/*cout << "Hello from the other side\n\n";
cout << " I've must have called a thousand times.\n\n";*/
int option = '0';
int choice = 'n';// To hold menu choices
bool cal_continue = true; // for the quit part
//bool radius_val = false; // validation for radius
/*while (radius < 0) // validation for radius)
{
cout << "Enter postive values please:";
cin >> radius;
}*/
do {
cout << " This program is a simple geometry calculator that will compute\n\n"; // display the directions and intent of the program
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 << ": 1 - 4 --> \n";
cin >> option;
if (option >= 1 && option <= 4)// The if for menu choices
{
if (option == 1)//The if for the circle block
{
constdouble PI = 3.14159;// const for pi
longdouble circleArea;// variable to hold cirlce area equation
longdouble radius;// user input variable
cout << " Enter the radius :\n";// Tells the suer to enter radius of circle
cin >> radius;//user enters radius
while (radius < 0) // validation for radius)
{
cout << "Enter postive values please:";
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 (option == 2) // Display the output and choices #2 Rectangle
{
longdouble length;//Variables for the lenght, width, and output
longdouble width;
longdouble recOutput;
cout << " Enter the length of rectangle: ";
cin >> length;
whilevalidate();
cout << " Enter the width of rectangle: ";
cin >> width;
whilevalidate();
recOutput = length * width;
cout << " The area of the rectangle is " << setprecision(7) << showpoint << recOutput << " .\n";
}
elseif (option == 3) //For the triangle
{
longdouble triBase;//Variables for triangle base, height, output
longdouble triHeight;
longdouble triOutput;
constdouble triangleopp = .5;// constant variable to hole opperand .5
cout << " Enter the Base: ";
cin >> triBase;
whilevalidate();
cout << " Enter the Height: ";
cin >> triHeight;
whilevalidate();
triOutput = (triBase * triHeight) / triangleopp;
cout << "The area of the triangle is " << setprecision(5) << showpoint << triOutput << ".\n";
}
else
{
cout << "exit\n\n";
cal_continue = false;
}
}
} while (cal_continue);
}
int main()
{
geoCalculator();
}