Как я могу сохранить результат getYearOfBirth, опции 1 и 2 в основной функции и использовать его для опции 3, где распечатать результат как квитанцию.
Какую проблему я обнаружил:
1) если я поставлю, например:
int y;
y=getYearOfBirth
когда я запускаю до тех пор, пока функция, в которой пользователь может повторно ввести yearOfBirth, он напечатает 1-й ввод вместо повторного ввода ввода
2) в варианте 1 у меня есть много значений для возврата, totalSum, количество1, количество2, количество3, количество4, я не знаю, как вернуть его в основную функцию
Это мой код:
отредактировано, добавлено 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;
}