How can I use the same loop multiple times in a function
Hello,
I am currently self learning how to use functions to better my programming abilities.
I am trying to create a program that uses a function, checks if the user input is between 2 values and prints it out.
I have a total of Four values i want to input. ie. 1,2,3,4
Each of the Four values has separate "low" "high" values.
ie. between "1" and "9" or between "0" and "9" etc..
In the end it should print something like this
value-1
"the value you have entered is: 1"
value-2
"the value you have entered is: 2"
value-3
"the value you have entered is: 3"
value-4
"the value you have entered is: 4"
My problem right now is trying to figure out how i can advance to the next print statement in the loop with a new range of "high" and "low" values.
here is my code so far.
Code:
#include <stdio.h>
int getUserInput(int, int);
int main(){
getUserInput(1, 9);
getUserInput(0, 9);
getUserInput(0, 9);
getUserInput(1, 8);
return 0;
}
int getUserInput(int low, int high) {
int goodValue;
while(goodValue < low || goodValue > high) {
printf("Enter a value between 1 and 9 inclusive: ");
scanf("%d", &goodValue);
if (goodValue >= low && goodValue <= high){
printf("the value you have entered is: %d\n", goodValue);
}
else {
printf("***error: %d is not valid, number must be between 1 and 9, try again***\n", goodValue);
}
}
return goodValue;
}