1

我已经在顶部的注释块中记录了程序规范。在函数 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;
}
4

1 回答 1

2

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

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

if (passValue < MIN_ACCEPTABLE || passValue > MAX_ACCEPTABLE)
{
    // Report the error somehow
}

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

如果您的意图是将 a 转换char *为 int,请查看strtol

于 2012-06-09T02:25:59.623 回答