1
0
Fork 0
SDEV-240-81/week-06/Week6_choice.cpp

38 lines
934 B
C++

// Lab 6 choice.cpp
// This program illustrates how to use a value-returning
// function to get, validate, and return input data.
// Llewellyn van der Merwe
#include <iostream>
#include <cmath>
using namespace std;
// Function prototype
int getChoice(int min, int max);
/***** main *****/
int main()
{
int choice;
cout << "Enter an integer between 1 and 4: ";
// WRITE A LINE OF CODE TO CALL THE getChoice FUNCTION AND TO
// ASSIGN THE VALUE IT RETURNS TO THE choice VARIABLE.
choice = getChoice(1, 4);
cout << "You entered " << choice << endl;
}
/***** getChoice *****/
int getChoice(int min, int max)
{
int input;
// Get and validate the input
cin >> input;
while (input < min || input > max)
{ cout << "Invalid input. Enter an integer between " << min << " and " << max << ": ";
cin >> input;
}
return input;
}