如何将getYearOfBirth,选项1和2的结果存储到主函数中,并将其用于选项3,在其中将这些结果打印为收据。

我发现的问题是:

1)如果我放,例如:

int y;
y=getYearOfBirth


当我运行直到用户可以重新输入yearOfBirth的函数时,它将打印第一个输入,而不是重新输入

2)在选项1中,我有很多要返回的值,totalSum,quantity1,quantity2,quantity3,quantity4,我不知道如何将其返回到主函数

这是我的代码:

编辑,添加y = getYearOfBirth()

#include <stdio.h>
#include <math.h>
void printWelcome(void); //print welcome
int getYearOfBirth (void); // prompt user to enter a year of birth , below 1900 show no valid
int confirmYob (void); // check if the year of birth is enter correctly
void displayMenu (void); //in option1 show table of product,with code and price
int getTotalSum (void); // when user selected product will calculate sum
int confirmTotalSum (void); // if the user make mistake can Re-enter the product purchase
int parkingCalculator (void); //enter the parking hour and calculate amount that need to pay
int confirmParkingFee (void); // if the user make mistake can Re-enter the parking hour
int main ()
{
int choice,y;
printWelcome();
y = getYearOfBirth();
confirmYob();
while(1)
{
    printf("Select an Action:\n");
    printf("1.Enter product purchased\n");
    printf("2.Enter parking hours\n");
    printf("3.Calculate and print the total bill\n");
    printf("4.Exit\n\n");
    scanf("%d",&choice);

    switch(choice)
    {
    case 1:
        {
        displayMenu();
        getTotalSum();
        confirmTotalSum();
        }
        break;
    case 2:
        parkingCalculator();
        confirmParkingFee();
        break;
    case 3:
        {
        printf("Test3\n\n");
        printf("%d\n\n",y)
        break;
        }
    case 4:
        exit(0);
    }
}
return 0;
}
void printWelcome(void)
{
printf("Welcom to :-\n");
printf("+-----------------------+\n");
printf("|     BIGSALES MALL     |\n");
printf("+-----------------------+\n");
}
int getYearOfBirth (void)
{
int yearOfBirth;

printf("Please enter your year of birth:\n");
scanf_s("%d", &yearOfBirth);
 while (yearOfBirth <1900)
    {
        printf("Warning! The year you entered is not valid!\n");
        printf("Please Enter Again.\n");
        printf("Please enter your year of birth:\n");
        scanf_s("%d", &yearOfBirth);
    }
printf("Your year of birth is: %d\n",yearOfBirth);
return yearOfBirth;
}
int confirmYob (void)
{
int confirm1;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter
\n");
scanf("%d",&confirm1);
while( confirm1 ==0 )
{
    getYearOfBirth();
    printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-
Enter) \n");
    scanf("%d",&confirm1);
}
    printf("Year of birth confirmed\n\n\n");
return 0;
}
void displayMenu (void)
{
printf("+--------------+--------------------+\n");
    printf("| Product Code | Rentail Price (RM) |\n");
    printf("+--------------+--------------------+\n");
    printf("| 1            | 45.20              |\n");
    printf("+--------------+--------------------+\n");
    printf("| 2            | 14.50              |\n");
    printf("+--------------+--------------------+\n");
    printf("| 3            |  3.45              |\n");
    printf("+--------------+--------------------+\n");
    printf("| 4            |  7.80              |\n");
    printf("+--------------+--------------------+\n");
}
int getTotalSum (void)
{
int code;
float sum=0,totalSum,quantity1=0,quantity2=0,quantity3=0,quantity4=0;
while(1)
{
    printf("Enter a product code.(Enter 5 to get total sum)\n");
    scanf("%d",&code);

    switch(code)
    {
    case 1:
        {
        quantity1=( quantity1 + 1);
        sum=( 45.20 + sum);
        printf("Current Sum: RM%.2f\n",sum);
        break;
        }
    case 2:
        {
        quantity2=( quantity2 + 1);
        sum=(14.50 + sum);
        printf("Current Sum: RM%.2f\n",sum);
        break;
        }
    case 3:
        {
        quantity3=( quantity3 + 1);
        sum=(3.45 + sum);
        printf("Current Sum: RM%.2f\n",sum);
        break;
        }
    case 4:
        {
        quantity4=( quantity4 + 1);
        sum=(7.80 + sum);
        printf("Current Sum: RM%.2f\n",sum);
        break;
        }
    case 5:
        totalSum= sum;
        printf("+--------------+--------------------+----------+\n");
        printf("| Product Code | Rentail Price (RM) | Quantity |\n");
        printf("+--------------+--------------------+----------+\n");
        printf("| 1            | 45.20              | %.f        |\n",quantity1);
        printf("+--------------+--------------------+----------+\n");
        printf("| 2            | 14.50              | %.f        |\n",quantity2);
        printf("+--------------+--------------------+----------+\n");
        printf("| 3            |  3.45              | %.f        |\n",quantity3);
        printf("+--------------+--------------------+----------+\n");
        printf("| 4            |  7.80              | %.f        |\n",quantity4);
        printf("+--------------+--------------------+----------+\n");
        printf("Total Sum: RM%.2f\n",totalSum);
        return 0;
    }
    }
}
int confirmTotalSum (void)
{
int confirm2;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter \n");
scanf("%d",&confirm2);
while( confirm2 ==0 )
{
    getTotalSum();
    printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-Enter) \n");
    scanf("%d",&confirm2);
}
    printf("Total Sum confirmed\n\n\n");
return 0;
}
int parkingCalculator (void)
{
float parkingHours, totalHours,parkingFee,maxParkingFee;
printf("Enter a parking hour\n");
scanf("%f",&parkingHours);
totalHours = ceil(parkingHours);
if (totalHours>24)
{
    parkingFee=0;
    printf("Parking Fee: RM %.2f\n\n",parkingFee);
}
else {if (totalHours==24)
     {
      parkingFee=10;
      printf("Parking Fee: RM %.2f\n\n",parkingFee);
     }
else {if (totalHours>3)
     {
      parkingFee=(2+((totalHours-3)*0.50));
      if (parkingFee>10)
      {
         parkingFee=10;
      }
      printf("Parking Fee: RM %.2f\n\n",parkingFee);
     }
else {if (totalHours<=3 && totalHours>=1)
     {
      parkingFee=2;
      printf("Parking Fee: RM %.2f\n\n",parkingFee);
     }
else {if(totalHours==0)
     {
      parkingFee=0;
      printf("Parking Fee: RM %.2f\n\n",parkingFee);
     }

}
}
}
}
return parkingFee;
}
int confirmParkingFee (void)
{
int confirm3;
printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter \n");
scanf("%d",&confirm3);
while (confirm3 >1)
{
    printf("Error");
}
while( confirm3 ==0 )
{
    parkingCalculator();
    printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-Enter) \n");
    scanf("%d",&confirm3);
}
    printf("Parking Fee confirmed\n\n\n");
return 0;
}

最佳答案

仔细执行程序:


在第15行上设置了y,从第一次调用到getYearOfBirth()
然后在第16行呼叫confirmYob()
confirmYob()在第82行上第二次调用getYearOfBirth()
第82行上对getYearOfBirth()的调用结果从不存储
您将打印y的值(在第15行设置;请参阅#1),因此,当然,它将打印您在第一次调用getYearOfBirth()时提供的值


同样,这是我的一个最小的例子,它着眼于当前的问题;这与大型程序使用的YoB逻辑相同,但更容易发现问题:

#include <stdio.h>
#include <math.h>

int getYearOfBirth (void); // prompt user to enter a year of birth , below 1900 show no valid
int confirmYob (void); // check if the year of birth is enter correctly

int main () {
    int y;

    y = getYearOfBirth();
    confirmYob();

    printf("%d\n\n",y);

    return 0;
}

int getYearOfBirth (void) {
    int yearOfBirth;

    printf("Please enter your year of birth:\n");
    scanf("%d", &yearOfBirth);
    while (yearOfBirth <1900) {
        printf("Warning! The year you entered is not valid!\n");
        printf("Please Enter Again.\n");
        printf("Please enter your year of birth:\n");
        scanf("%d", &yearOfBirth);
    }

    printf("Your year of birth is: %d\n",yearOfBirth);
    return yearOfBirth;
}

int confirmYob (void) {
    int confirm1;
    printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter\n");
    scanf("%d",&confirm1);
    while (confirm1 == 0) {
        getYearOfBirth();
        printf("Is it correct? (Enter 1 for YES to Continue ,0 for NO and Re-Enter) \n");
        scanf("%d",&confirm1);
    }

    printf("Year of birth confirmed\n\n\n");

    return 0;
}


这是我的修复建议:一次调用getYearOfBirth()其中的一个关键部分是在getYearOfBirth()函数内部包括确认逻辑,因此,如果需要重新输入,则仍在同一调用中getYearOfBirth()

#include <stdio.h>
#include <math.h>

int getYearOfBirth (void); // prompt user to enter a year of birth , below 1900 show no valid

int main () {
    int y;

    y = getYearOfBirth();

    printf("%d\n\n",y);

    return 0;
}

int getYearOfBirth (void) {
    int yearOfBirth;
    int confirm1;

    while (1) {
        printf("Please enter your year of birth:\n");
        scanf("%d", &yearOfBirth);

        if (yearOfBirth < 1900) {
            printf("Warning! The year you entered is not valid!\n");
            printf("Please Enter Again.\n");
            continue;
        }

        printf("Your year of birth is: %d\n",yearOfBirth);

        printf("Is it correct? Enter 1 for YES to Continue ,0 for NO and Re-Enter\n");
        scanf("%d",&confirm1);
        if (confirm1 == 1) {
            printf("Year of birth confirmed\n\n\n");
            break;
        }
    }

    return yearOfBirth;
}

10-01 13:17