return 选项 1 的多个输出,如何将其存储在主函数中并将其用于选项 3

return multiple output from option 1 , how to store it at main function and use it for option 3

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

我发现的问题是:

1) 如果我输入,例如:

int y;
y=getYearOfBirth

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

2) 在选项 1 中,我对 return 有很多价值,总和,数量 1,数量 2,数量 3,数量 4,我不知道如何 return 它到主要功能

这是我的代码:

已编辑,添加了 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;
}

非常仔细地跟踪你程序的执行:

  1. y设置在第15行,从第一次调用到getYearOfBirth()

  2. 然后你在第 16 行调用 confirmYob()

  3. confirmYob() 在第 82

  4. 行对 getYearOfBirth() 进行 第二次 调用
  5. 永远不会存储第 82 行对 getYearOfBirth() 的调用结果

  6. 您打印 y 的值(在第 15 行设置;参见 #1),因此它当然会打印您在 1st 调用 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() 一次 其中一个关键部分是包含确认逻辑 inside 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;
}