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
}

パラメータが整数型であるため、書かれた関数は「悪い」(非整数のような)入力を許可しません。

あなたの意図が achar *を int に変換することである場合は、strtolを見てください。

于 2012-06-09T02:25:59.623 に答える