-1

当函数 recordWeight() 被执行并且我通过 scanf 输入重量时,它可以工作,但最后它说“无效输入”,这是 switch Choice 中使用的函数 getChoice 的 printf,这个“无效输入”消息是 ment仅在您为开关选择错误选项时出现。它不应该影响开关中的功能。我认为它可能会阻止值出现在 displayHistory() 函数中......我真的很感激任何帮助!

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define pause system("pause")
#define cls system("cls")
#define SIZE 50
#define flush fflush(stdin)


char getChoice();
void displayMenu();
void recordWeight(float a[], int *c);
void displayAverage(float a[], int c);
void highLow(float a[], int c);
void displayHistory(float a[], int c);


main(){
    int counter = 0;
    float weight[SIZE]={0.0};
    char choice;

    do {
        choice = getChoice();
        switch(choice){

        case 'A':
            recordWeight(weight,&counter);
            break; 
        case 'B':
            displayAverage(weight,counter);
            break;
        case 'C':
            highLow(weight, counter);
            break;
        case 'D':
            displayHistory(weight, counter);
            break;
        }//end switch

    }while (choice != 'E' );
}//end main

char getChoice(){
    char result = 0;
    do{
        displayMenu();
        scanf_s("%c", &result);
        flush;
        if(result != 'A' && result != 'B' && result != 'C' && result != 'D' && result != 'E'){
            printf("Invalid Selection\n");
        pause;}
    } while(result != 'A' && result != 'B' && result != 'C' && result != 'D' && result != 'E');
    return result;
}//end getChoice

void displayMenu(){
    cls;
    printf("Main Menu\n");
    printf("A) Record Weight\n");
    printf("B)Display Average Weight\n");
    printf("C)Display Highest and Lowest Weight\n");
    printf("D)History of recorded Weight\n");
    printf("E)QUIT!!!!\n\n");

    printf("Please Enter your selection: \n");
    return;
}//end displayMenu

void recordWeight(float *a,int *c){
    printf("please enter a your weight..\n");
    scanf("%f", &a[*c]);
    *c = *c + 1;
    if(c > 0){
    if(a[*c]>a[*c-1])
        printf("Good, You gained weight!\n");
    else if(a[*c] < a[*c-1])
        printf("Ew You lost weight!\n");
    else 
        printf("Your still the same weight as before...gain some weight!\n");
        pause;
    }
}//end recordWeight

void displayAverage(float a[], int c){
    float average, total = 0.0;
    int i;
    if(c> 0){
    for(i=0; i < c; i++)
        total = total + a[i];
     average = total/ c;
     printf("\nYour Average weight is %.2f\n",average);
     pause;}
    else
        printf("You must enter atleast one weight in order to use this function.");
    pause;
}

void highLow(float a[],int c){
    if(c > 0){
    float high= a[0], low= a[0];
    int i;
    for(i=0; i < c; i++){
        if(a[i]> high)
            high= a[i];
        if(a[i]<low)
            low = a[i];
    }
    printf("The thinest you have been is %i pounds\n", low);
    printf("The Fattest you have been is %i pounds\n", high);
    pause;}
    else 
        printf("You must enter atleast one weight in order to use this function.\n");
    pause;
}//end highLow

void displayHistory(float a[],int c){
    int i;
    if(c > 0){
    for(i=0;i < SIZE; i++)
        printf(" %i. You were %i pounds\n",i+1,a[i]);
    }
    else
        printf("You must enter atleast one weight in order to use this function.\n");
    pause;
}//end displayHistory
4

1 回答 1

3

您的问题是权重的输入将换行符留在输入缓冲区中。因此,您的"%c"格式会获得换行符,它不是 A 或 B 或 C 或 D 或 E 并触发警告。

您最简单的解决方法是在;" %c"之前使用带有空格的格式。%c这将在读取非空白字符之前跳过空格,包括换行符。

于 2013-11-02T06:53:23.733 回答