gpt4 book ai didi

c - 我的交换机的功能的 scanf 对其中的功能产生了不利影响

转载 作者:行者123 更新时间:2023-11-30 18:41:51 25 4
gpt4 key购买 nike

当函数 recordWeight() 被执行并且我通过 scanf 输入重量时,它可以工作,但最后它说“无效输入”,这是来自 switch Choice 中使用的函数 getChoice 的 printf,这个“无效输入”仅当您为交换机选择了错误的选项时,才会出现该消息。它不会影响交换机的功能。 我认为这可能会阻止值出现在 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

最佳答案

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

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

关于c - 我的交换机的功能的 scanf 对其中的功能产生了不利影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19739616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com