-1

How can I store the result of getYearOfBirth ,option 1 and 2 to main function and use it for option 3 where to print those result as receipt.

What problem i have found is:

1) if I put , for example :

int y;
y=getYearOfBirth

when I run until the function where user can re-enter the yearOfBirth , it will print the 1st input instead of re-enter input

2) in option 1 , I have many value to return , totalSum, quantity1,quantity2,quantity3,quantity4, i dont know how to return it to main function

This is my code:

edited ,added 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;
}
zhezhi chew
  • 15
  • 1
  • 5
  • Try changing your `scanf`'s to escape the newline character by adding a space before the %. Something like this -> `scanf(" %d", foo);` – Haris Nov 09 '18 at 04:45
  • 1
    it sounds like you posted code that *does* work, and just *described* the code that doesn't work; it will be easier to help you if (1) you reduce the code to just the part in question (the year-of-birth logic; you've posted a lot that has nothing to do with that), and (2) post the code that *isn't* working, and then point out where in the code you expect something different to happen – landru27 Nov 09 '18 at 04:59
  • @Haris all of scanf ? – zhezhi chew Nov 09 '18 at 06:20
  • @landru27 sorry , I didn't explain well , this code is 'incomplete' , all inputs and processes are in my expectation but I don't know how to make the output at option 3 in main function. The procedure is : when user choose 1 at main function , user select product code ,then confirm the product and amount, save it as the input of option 3. After that , select option 2 , calculate parking fee , save it as the input of option 3. Last , in option 3 , receive input from option 1 and 2 and output as a receipt . – zhezhi chew Nov 09 '18 at 06:23
  • well, you were on the right track with `y = getYearOfBirth` ... the only way to output a value later is to store it earlier, when it is calculated; `int y` also looks correct, and is necessary for `y = getYearOfBirth` to work; but you don't have those in the code you posted, you only *describe* using them; put them into your code so that we can see clearly what you're doing wrong when you use them – landru27 Nov 09 '18 at 06:28
  • @landru27 I have edited as you said , so here is the result : [link](https://drive.google.com/open?id=16bf9CCgVzhEVIfyjq_75zAT21QITqDzS) this prolem occur when I try to re-enter , Explaination: 1) I entered 1900 as my YOB ,2) select 0 because I entered a wrong one , 3) I input a correct one , 4) so I sure it is enter correctly, 5)select 1 because it is correct , 6) it print 1900 which it is my 1st entered number – zhezhi chew Nov 09 '18 at 07:02
  • now I see the problem very clearly; I will post an answer – landru27 Nov 09 '18 at 07:16

1 Answers1

0

Follow the execution of you program very carefully:

  1. y is set on line 15, from the 1st call to getYearOfBirth()

  2. then you call confirmYob() on line 16

  3. confirmYob() makes a 2nd call to getYearOfBirth() on line 82

  4. the results of the call to getYearOfBirth() on line 82 are never stored

  5. you print the value of y (set on line 15; see #1), so of course it will print the value you provided during the 1st call to getYearOfBirth()

Also, here is what I meant by a minimal example that focuses on the problem at hand; this uses the same logic for YoB as your larger program, but makes is much easier to see the problem:

#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;
}

Here is my suggestion for fixing it : call getYearOfBirth() just once A key part of this is including the confirmation logic inside the getYearOfBirth() function, so that if it needs to be re-entered, you are still within the same call to 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;
}
landru27
  • 1,654
  • 12
  • 20
  • Thank you for your answer, it works. Can you help me in another function in my code , Here is the code I have minimize : [link](https://drive.google.com/open?id=1ZULmFKIvyTJWoyarxxIGUS9m7RCjAQRN). This code have multiple return , for example , return totalSum, quantity1,quantity2,quantity3,quantity4, and I also want it to return to main function. how can I fix it? – zhezhi chew Nov 09 '18 at 08:51
  • @zhezhichew I'll be happy to help! for a separate problem, please open a new question; SO is community-driven and a new question will allow others to help, too; also, a new question will let you post a new [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) – landru27 Nov 09 '18 at 12:29
  • @zhezhichew : I just now saw your new question about multi return values; the answer from Fiddling Bits is exactly what I had in mind; it's a great answer, and I'm glad you received the help you needed! – landru27 Nov 09 '18 at 17:38