I’ve documented the program specifications in the comment block located at the top. In the function displayNameByValue, I’m passing in an integer variable called passValue that stores the number of times the user’s name will be displayed to the console. I want to be able to account for user input error and validate for any input the user passes in that’s not represented by an integer. What’s the best possible solution to handle this situation?
Here’s my code :
/*******************************************************************************
Concept:
1.) Display the Programmer's Name
2.) Display the Programmer's Name Again
3.) Display the Programmer's Name X Times
4.) Display a Triangle of an Entered Character
5.) Exit the Program
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
char displayMenu();
void displayName(char *userName);
void displayNameAgain(char *userName);
int displayNameByValue(char *userName, int passValue);
char *userName = "Demetrius \n";
int main()
{
char menuOption;
int passValue = 0;
menuOption = displayMenu();
while(menuOption != 'E')
{
switch(menuOption)
{
case 'A':
displayName(userName);
break;
case 'B':
displayNameAgain(userName);
break;
case 'C':
displayNameByValue(userName, passValue);
break;
case 'D':
// currently working @ the moment...
break;
default:
printf("You've entered an invalid character entry! \n\n");
break;
}
menuOption = displayMenu();
}
system("pause");
return 0;
}
char displayMenu()
{
char menuChoice;
printf("**********************************************************\n");
printf("A. Display the Programmer's Name *\n");
printf("B. Display the Programmer's Name Again *\n");
printf("C. Display the Programmer's Name X times *\n");
printf("D. Display a Triangle of an Entered Character *\n");
printf("E. Exit the Program *\n");
printf("**********************************************************\n\n");
printf("Enter a character that corresponds to the menu above :\n");
scanf("%s", &menuChoice);
menuChoice = toupper(menuChoice); // Assigns all menu submissions to uppercase
return menuChoice;
}
void displayName(char *userName)
{
printf("%s", userName);
}
void displayNameAgain(char *userName)
{
printf("The programmer's name is : %s" , userName);
}
int displayNameByValue(char *userName, int passValue)
{
int index;
printf("Enter the number of times to display your name :");
scanf("%d", &passValue);
for(index = 0; index < passValue; index++)
{
printf("%s\n", userName);
}
printf("Your name was displayed : %d times\n", index);
return passValue;
}
Since the parameter
int passValueis, well, an integer it will ALWAYS (by definition) be somewhere in the range INT_MIN to INT_MAX.If you are instead interested in making sure the value is in a narrower range (say at least 1 and less than 100), you could define constants in your program for the MIN_ACCEPTABLE and MAX_ACCEPTABLE values, and test like
The function as written does not allow for “bad” (as in non-integer) input because the parameter is of type integer.
If your intent is to convert a
char *to an int, have a look at strtol.