function call help using while loop for validation

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.

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
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
using namespace 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
			{


				const double PI = 3.14159;// const for pi
				long double circleArea;// variable to hold cirlce area equation
				long double 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;
			}



			else if (option == 2)  // Display the output and choices #2 Rectangle
			{
				long double length;//Variables for the lenght, width, and output
				long double width;
				long double 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";


			}

			else if (option == 3)  //For the triangle
			{
				long double triBase;//Variables for triangle base, height, output
				long double triHeight;
				long double triOutput;
				const double 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();






}
Last edited on
What exactly are you trying to validate? (Why are you asking the user for input within that function?)

Where are you even attempting to use the return value of the function?

1
2
int userChoice = 0;
while (userChoice < 0) // validation for radius) 

The while loop will never be executed.
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
i just transferred the same to a a function.
I have the return there because its return the value(userChoice) to its call

And where are you actually using the return value from that function?

The intent is to have it returned to geo calcuator function when its called since its variable is dependent on user input.
But you're not using the return value, plus you're also calling this function to "validate" radius that uses a floating point number, not an int.

Lastly for now if you are going to use it to validate the radius then shouldn't you be "validating" the value already entered by the user in main()?

Something more like the following perhaps?
1
2
3
4
5
6
7
8
9
10
double validateInput(double numberToValidate)
{
    while(numberToValidate < 0.0)
    {
        std::cout << "Please enter a positive number: ";
        std::cin >> numberToValidate);
    }

    return numberToValidate;
}



Edit:
By the way why are you using long double? A simple double should be adequate.

Last edited on
That doesn't work. I get an error saying too few arguments.
I did with and without the parameters
Last edited on
show how you tried to use it, his code looks fine.

should be something like
double radius = validateInput(-1.0); //lazy me, this lets the validation do the prompt :)

or less lazy
double radius = -1.0;
std::cout << "Please enter a positive number: ";
cin >> radius;
radius = validateInput(radius);
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
using namespace 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
			{


				const double PI = 3.14159;// const for pi
				long double circleArea;// variable to hold cirlce area equation
				long double 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;
			}



			else if (option == 2)  // Display the output and choices #2 Rectangle
			{
				long double length;//Variables for the lenght, width, and output
				long double width;
				long double 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";


			}

			else if (option == 3)  //For the triangle
			{
				long double triBase;//Variables for triangle base, height, output
				long double triHeight;
				long double triOutput;
				const double 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();






}
I get an error within the parameters, even when I add the parameters there it doesn't compile.
It seems to work just fine for me?

If you're getting errors then post the complete error message, all of them, exactly as it appears in your development environment.


And again why are you using the long double?


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.
How does it run just fine for you?
Well, posting the errors, the complete error messages, you receive would help up to be able to help you.

When I run and I input a negative value the program compiles it.
How can you run a program you state does NOT compile?

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
			{
				const double 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.)


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
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <fstream>
 
using namespace 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
			{


				const double PI = 3.14159;// const for pi
				long double circleArea;// variable to hold cirlce area equation
				 long double 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;
			}



			else if (option == 2)  // Display the output and choices #2 Rectangle
			{
				long double length;//Variables for the lenght, width, and output
				long double width;
				long double 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";


			}

			else if (option == 3)  //For the triangle
			{
				long double triBase;//Variables for triangle base, height, output
				long double triHeight;
				long double triOutput;
				const double 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();
}
Last edited on
Topic archived. No new replies allowed.