在C中验证整数值

时间:2012-06-09 02:08:30

标签: c

我在位于顶部的注释块中记录了程序规范。在函数displayNameByValue中,我传入一个名为passValue的整数变量,该变量存储用户名称将显示到控制台的次数。我希望能够解决用户输入错误并验证用户传递的任何输入,而不是由整数表示。处理这种情况的最佳解决方案是什么?

这是我的代码:

/*******************************************************************************
 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;
}

1 个答案:

答案 0 :(得分:2)

由于参数int passValue是一个整数,它总是(按定义)在INT_MIN到INT_MAX的范围内。

如果您有兴趣确保该值在较窄的范围内(例如至少为1且小于100),则可以在程序中为MIN_ACCEPTABLE和MAX_ACCEPTABLE值定义常量,并像

一样进行测试
if (passValue < MIN_ACCEPTABLE || passValue > MAX_ACCEPTABLE)
{
    // Report the error somehow
}

写入的函数不允许输入“bad”(如非整数),因为参数的类型为整数。

如果您打算将char *转换为int,请查看strtol