初学者到C并为课程做作业。修复了我认为需要修复的问题,但我的菜单只会转到A部分,而不是B,C或D.它也会退出。没有错误,想知道我的代码有什么问题。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH flush()
// Prototype Functions Here
void highestNumber(int, int, int);
void lowestNumber(int, int, int);
void displayAverage(int, int, int);
void displayMenu();
void flush();
char getUserChoice();
main() {
char userSelection;
int number1 = 0, number2 = 0, number3 = 0;
do {
userSelection = getUserChoice(); // Gets/DisplaysMenu
switch (userSelection) {
case 'A': // Enter a number
printf("Enter a number 1: ");
scanf("%i", &number1);
printf("Enter a number 2: ");
scanf("%i", &number2);
printf("Enter a number 3: ");
scanf("%i", &number3);
break;
case 'B': // Display Highest Number Entered
printf("In Case B");
highestNumber;
break;
case 'C': // Display Lowest Number entered
printf("In Case C");
lowestNumber;
break;
case 'D': // Display Average of Numbers entered
printf("In Case D");
displayAverage;
break;
case 'Q': // Quit the program
printf("You have quit the program\n");
PAUSE;
break;
default: // Invalid Selection
printf("Invalid selection...try again!\n");
PAUSE;
break;
} // end switch
} while (userSelection != 'Q');
PAUSE;
} // end of main
//==============
void highestNumber(int number1, int number2, int number3) {
if (number1 > number2 && number1 > number3) {
printf("The highest number is: %i", number1);
}
else if (number2 > number1 && number2 > number3) {
printf("The highest number is: %i", number2);
}
else if (number3 > number2 && number3 > number1) {
printf("The highest number is: %i", number3);
}
} //end highest number
void lowestNumber(int number1, int number2, int number3) {
if (number1 < number2 && number1 < number3) {
printf("The lowest number is: %i", number1);
}
else if (number2 < number1 && number2 < number3) {
printf("The lowest number is: %i", number2);
}
else if (number3 < number2 && number3 < number1) {
printf("The lowest number is: %i", number3);
}
} // end lowest number
void displayAverage(int number1, int number2, int number3) {
int average = (number1 + number2 + number3) / 3;
printf("The average of the three numbers you entered is: %i", average);
} // display average
void displayMenu() {
CLS;
printf("\n===============================\n");
printf("========== MAIN MENU ==========\n");
printf("===============================\n");
printf("A. Enter a number\n");
printf("B. Display Highest Number entered\n");
printf("C. Display Lowest Number entered\n");
printf("D. Display Average of Numbers entered\n");
printf("Q. Quit the program\n");
printf("Enter your choice: ");
} // end displayMenu
void flush() {
while (getchar() != '\n');
} // end flush
char getUserChoice() {
char result;
displayMenu();
scanf("%c", &result); FLUSH;
result = toupper(result);
return result;
} // end getUserChoice
答案 0 :(得分:0)
以下代码应该可以根据需要执行。
警告:您可能希望修改一些细节,例如取消注释getchar()
的调用
以下代码构建了许多约束,例如在尝试对这些数字执行任何操作之前必须输入3个数字。
以下代码不使用对shell的非可移植调用。
数据保存在main()
函数的本地,并传递给需要它的每个子函数。
请注意,更改数据的函数会传递指向数据的指针,因此子函数可以更改该数据。
消除了无用的宏函数:flush()
已重命名,以明确它的作用
添加了适当的错误检查
注释掉了不需要的if()
语句
展开flushStdin()
以允许EOF或&#39; \ n&#39;
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Prototype Functions Here
void enterNumber (int*, int*, int*); // pass address so function can update
void highestNumber (int, int, int);
void lowestNumber (int, int, int);
void displayAverage(int, int, int);
void displayMenu ( void ); // corrected prototypes
void flushStdin ( void );
char getUserChoice ( void );
int main( void ) // only two valid signatures for main()
// this is one of them
{
char userSelection;
int number1; // added local data variables
int number2;
int number3;
int gotSelections = 0; // so cannot try to work with uninitialized data
do
{
userSelection = getUserChoice(); // Gets/DisplaysMenu
switch (userSelection)
{
case '1': // Enter a number
enterNumber( &number1, &number2, &number3 ); // pass expected parameters
gotSelections = 1;
break;
case '2': // Display Highest Number Entered
if( gotSelections )
{ // then data initialized
highestNumber( number1, number2, number3 ); // pass expected parameters
}
else
{ // else tried to pass unitialized data
printf( "must enter numbers before gathering Statistics\n" );
}
break;
case '3': // Display Lowest Number entered
if( gotSelections )
{ // then data initialized
lowestNumber( number1, number2, number3 ); // pass expected parameters
}
else
{ // else tried to pass unitialized data
printf( "must enter numbers before gathering Statistics\n" );
}
break;
case '4': // Display Average of Numbers entered
if( gotSelections )
{ // then data initialized
displayAverage( number1, number2, number3 );
}
else
{ // else tried to pass unitialized data
printf( "must enter numbers before gathering Statistics\n" );
}
break;
case 'Q': // Quit the program
printf("You have quit the program\n");
//system( "pause" );
break;
default: // Invalid Selection
printf("Invalid selection...try again!\n");
//system( "pause" );
break;
} // end switch
flushStdin();
//getchar();
} while (userSelection != 'Q');
//system( "pause" );
//flushStdin();
getchar();
} // end of main
void enterNumber(int * pNumber1, int *pNumber2, int *pNumber3)
{
printf("Enter a number 1:\n ");
if( 1 != scanf("%i", pNumber1) )
{
fprintf( stderr, "scanf for number1 failed" );
flushStdin();
getchar();
exit( EXIT_FAILURE );
}
printf("Enter a number 2:\n ");
if( 1 != scanf("%i", pNumber2) )
{
fprintf( stderr, "scanf failed for number2" );
flushStdin();
getchar();
exit( EXIT_FAILURE );
}
printf("Enter a number 3:\n ");
if( 1 != scanf("%i", pNumber3) )
{
fprintf( stderr, "scanf for number3 failed" );
flushStdin();
getchar();
exit( EXIT_FAILURE );
}
} //end enter number
void highestNumber(int number1, int number2, int number3)
{
if (number1 > number2 && number1 > number3)
{
printf("The highest number is: %i\n", number1);
}
else if (number2 > number1 && number2 > number3)
{
printf("The highest number is: %i\n", number2);
}
else //if (number3 > number2 && number3 > number1)
{
printf("The highest number is: %i\n", number3);
}
} //end highest number
void lowestNumber(int number1, int number2, int number3)
{
if (number1 < number2 && number1 < number3)
{
printf("The lowest number is: %i\n", number1);
}
else if (number2 < number1 && number2 < number3)
{
printf("The lowest number is: %i\n", number2);
}
else //if (number3 < number2 && number3 < number1)
{
printf("The lowest number is: %i\n", number3);
}
} // end lowest number
void displayAverage(int number1, int number2, int number3)
{
int average = (number1 + number2 + number3) / 3;
printf("The average of the three numbers you entered is: %i\n", average);
} // display average
void displayMenu()
{
//system( "cls" );
puts( "\033[H \033[J"); // \033 is octal value for 'escape'
printf("\n===============================\n");
printf("========== MAIN MENU ==========\n");
printf("===============================\n");
printf("1. Enter a number\n");
printf("2. Display Highest Number entered\n");
printf("3. Display Lowest Number entered\n");
printf("4. Display Average of Numbers entered\n");
printf("Q. Quit the program\n");
printf("Enter your choice: ");
} // end displayMenu
void flushStdin()
{
int ch;
while ( (ch = getchar()) != EOF && '\n' != ch );
} // end flush
char getUserChoice()
{
char result;
displayMenu();
if( 1 != scanf(" %c", &result) )
{
fprintf( stderr, "scanf for menu entry failed" );
flushStdin();
getchar();
exit( EXIT_FAILURE );
}
flushStdin();
//getchar();
result = (char)toupper((int)result);
return result;
} // end getUserChoice