Multiple function use and returning a value from a function

I have to Write a function named inputVal() that prompts the user for an integer and uses a loop to validate it using the minimum given in the parameter set. Use the following function prototypes:
• int inputVal(int min, int sentinel);

o Parameters:
 int min: lower limit
 int sentinel: value to exit (return sentinel value so main program can check it!)

o Return value:
 Validated integer user input (or sentinel value if user is exiting)

Consider the following code, which calls the function and gets user input for int x that is 15 or greater:
int x = inputVal(15, -1);

Write a program named inputvalidation.cpp that:
• Tests the function
• Contains a sample execution using a multi-line comment (include invalid user inputs)

This is what the output is supposed to look like for reference
Task 1 Sample Output
Please enter an integer (min: 15, exit: -1): 13
Invalid input, try again!
Please enter an integer (min: 15, exit: -1): -10
Invalid input, try again!
Please enter an integer (min: 15, exit: -1): 17
Please enter an integer (min: 15, exit: -1): -1

I have been trying to do this, however, I am still very new to multiple functions and C++ altogether so I am having trouble constructing the function and getting it to work. I keep getting errors that says my inputVal function needs to return a value and I think I must be misinterpreting what it means because I thought I did that.

this is my code:
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
#include <iostream>
#include <cmath>
using namespace std;

int inputVal(int min, int sentinel)
{
	//Prompt user for integer value
	cout << "Please enter an integer (min: 15, exit: -1): ";
	//Get integer value
	int integer;
	cin >> integer;

	while (integer < 15)
	{
		cout << "invalid input, try again!";
		cin >> integer;
	}
	return sentinel;
}

int main()
{
	int x = inputVal(15, -1);
	system("pause");
	return 0;
}


I know its not much, but I couldn't figure out the error and I didn't want to continue just in case I made more errors along the way.
Any help would be greatly appreciated, thank you so much for your time!
Hi, those codes works fine on me, and for "getting errors that says my inputVal function needs to return a value", do you mind post the full error message?
By the way your "prompt" should look more like: cout << "Please enter an integer (min: " << min << ", exit: " << sentinel << "): ";

And your validation should not have those magic numbers (15 and -1). Hint you also need to check if the use entered the "sentinel" value.

You also should check for some totally invalid entry such as an alpha character (hint: check the stream state).

You may also want to clear() any stream errors and insure everything in the input buffer is ignored.

That's very strange because it is working for me now too. I don't understand why it is working all of the sudden and I am very sorry for wasting your time on that!

Another part of the program that I am having trouble understanding is returning a value to the main function. I think that I have it set to return the sentinel value to main (as seen in line 18) but I'm also not sure what the sentinel value is (or if it even has one at this point in my code).

My prompt works fine for what I need to do (its supposed to be very simple stuff I think), and so does my while loop. I've added to my code and this is what I have now:

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
#include <iostream>
#include <cmath>
using namespace std;

int inputVal(int min, int sentinel)
{
	//Prompt user for integer value
	cout << "Please enter an integer (min: 15, exit: -1): ";
	//Get integer value
	int integer;
	cin >> integer;

	if (integer < 15)
	{
		while (integer < 15)
		{
			if (integer == -1)
			{
				break;
			}

			else
			{
				cout << "invalid input, try again!";
				cin >> integer;
			}
		}
	}

	return sentinel;
}

int main()
{
	int x = inputVal(15, -1);
	system("pause");
	return 0;
}


I also need to make it so that the prompt loops until they enter a -1 and I was wondering what the best way to do that would be? I know its most likely a while loop, but I was wondering If the code I already have would need to be changed around because it looks messy to me (but that also might just be my beginner eyes, I'm not sure)
My prompt works fine for what I need to do

Only if the minimum value and sentinel don't change. You should be prompting the user to enter a value less than the min, or to enter the sentinel, both of these values are variable.

I also need to make it so that the prompt loops until they enter a -1 and I was wondering what the best way to do that would be?

Do you want to keep looping until they enter the sentinel (not always a -1)? Or do you want to keep looping until they enter value less than min or enter the sentinel?

I think that I have it set to return the sentinel value to main

Okay but then why are you asking the user for some value if you always return the sentinel (a value passed into the function)? Wouldn't it make more sense to return the value inputted, either a value less than min or the sentinel?

I know its most likely a while loop,

That would probably be a good choice, if you don't always return the sentinel value.

but I was wondering If the code I already have would need to be changed around because it looks messy to me

Most likely, and yes it is a little "messy" as you are repeating a "condition" in at least two places. Also you are not testing for some "horribly wrong" input like non-numeric values.



Last edited on
So what you're saying is that the line int inputVal(int min, int sentinel) means that min and sentinel are variables. So does that mean that I have to give them the values that I want? Because the minimum is supposed to be 15 and the sentinel (which I am supposed to use to exit the code) is -1 which would look like this min = 15; and then later sentinel = -1;?

I want to keep looping until they enter the -1, however if they enter something less than 15 then I need to enter another loop that gives out a message "invalid input try again". I only want to exit when they enter -1.

I'm not really sure why I have to return that value but I know that I have to because of this line from the worksheet:
 int sentinel: value to exit (return sentinel value so main program can check it!)

I'm also confused as to what my inputVal function is supposed to do and what I am supposed to leave for main if that makes any sense.

I know that I am repeating conditions but I needed to use break to stop the function and in order to use that it had to be a part of a loop I'm sure that there is a much better way to do this, but I'm not sure what this way is
Hi, I read the question again. The question says "prompts the user for an integer and uses a loop to validate it using the minimum given in the parameter set."
Also, the example it gives :

Please enter an integer (min: 15, exit: -1): 13
Invalid input, try again!
Please enter an integer (min: 15, exit: -1): -10
Invalid input, try again!
Please enter an integer (min: 15, exit: -1): 17
Please enter an integer (min: 15, exit: -1): -1

so it looks like the program will first prompts user with "Please enter an integer (min: 15, exit: -1):", and if user enter number lower than min(which is 15 in sample), the program will keep showing "Invalid input, try again!" and "Please enter an integer (min: 15, exit: -1):" until user enter number larger than 15 or -1(which is sentinel).

Therefore, you can imagine that there is an "infinite loop" keep showing "Please enter an integer (min: 15, exit: -1):" and "Invalid input, try again!" until you enter number larger than 15 or -1(which is sentinel), which can break the loop and jump out. After that, the function should return the valid value or -1.

i modify your first code with my imagine as below:
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
int inputVal(int min, int sentinel)
{	
	//Get integer value
	int integer;
        /*Below is infinite loop to keep saying "Please enter an integer (min: 15, exit: -1): " and "invalid 
           input, try again!"*/
	while (1)
	{
		//Prompt user for integer value
		cout << "Please enter an integer (min: 15, exit: -1): ";
		cin >> integer;
                
                /*check if user enter number larger than 15 or same as sentinel(-1) so that 
                   program can break and jump out of loop*/
		if(integer>=15||integer==sentinel)
		{
			break;
		}
		if(integer < 15)
		{
			cout << "invalid input, try again!";
		}		
		//cin >> integer;  // this line duplicated so i set it as comment
	}
	
        //after break the loop, we should assign the valid-user-input to sentinel, so it can return
	sentinel=integer;
	return sentinel;
}

int main()
{
	int x = inputVal(15, -1);
        /*in case you need to check what number user type, I output x, which is return value 
           from sentinel of function inputVal*/ 
	cout<<x<<endl;
	system("pause");
	return 0;
}


I didn't add carriage return, so it won't look like the same as sample. please help yourself to add it.

Also, I guess the last two line sample example:
Please enter an integer (min: 15, exit: -1): 17
Please enter an integer (min: 15, exit: -1): -1

which is showing correct case, so it doesn't have "Invalid input, try again!"
feel free to let me know if I misunderstand the question. thanks
Last edited on
Topic archived. No new replies allowed.