简体   繁体   中英

How to printf the value of input by user multiple times

I'm doing my c assignment and I have faced some problems. One of it is that I can't printf the value that are retrieved from the user. The value retrieved is in for loop and for the output it only shows the latest value that are entered by user. For this project, I can't use array or function. Someone please help me:(

printf("\n\nPlease Enter the number of bookings that you would like to make : ");
scanf("%d" , &noOfBooking );


//for control structures
for(i=1; i<=noOfBooking; i++){

    printf("\nBooking %d" , i);

    printf("\nSelect Room Type Option: ");
    scanf("%d" , &option);

    printf("Number of days: ");
    scanf("%d" , &days);

    printf("Number of person staying in the room: ");
    scanf("%d" , &noPerson);

    //selection structures
    if(option==1)
    {
        ratePerDay = 75.00;
        countSB--;

    }

    else if(option==2)
    {
        ratePerDay = 80.00;
        countQB--;
    }

    else if(option==3)
    {
        ratePerDay = 100.00;
        countKB--;
    }

    else if(option==4)
    {
        ratePerDay = 150.00;
        countFR--;
    }
    else 
        printf("You have entered invalid input");


        printf("Your Payment Booking are as follows :");
        printf("\nRoom Type Option %d", option);        

}

The expected output should be like this: -

Please Enter the number of bookings that you would like to make: 2

Booking 1

Select Room Type Option: 1

Number of days: 2

Number of person staying in the room: 1

Total Cost: 150.00

Booking 2

Select Room Type Option: 4

Number of days: 1

Number of person staying in the room: 3

Total Cost: 150.00

Your Payment Booking are as follows:

Room Type Option 1 (Single Bed) Total Cost is RM 150.00

Room Type Option 4 (Family Room) Total Cost is RM 150.00

I was thinking about using structs and theswitch/case statement to solve your problem as they are not arrays or functions and you could store all the required information and afterwards handle the if/else if statements. For example:

// structs
// arguments related to your code

struct Booking {
 int option;
 int days;
 int noPersons;
 int num_of_Booking;
};


// switch-case
switch(option){
  case 1:
    ratePerDay = 75.00;
    countSB--;
    break; // optional - to terminate the switch statement
  case 2:
     ...
   .
   .
  case x:
     ...
  default: // it works like the last else if your code
     printf("You have entered invalid input");

As Paul said please provide some additional information on what you are trying to achieve in order for me to be more specific.

I hope that this helps you.

You can put every value in the same variable, for example, you can put the number 9 between every value that entered by the user.

For example if the user entered the options (1,3,2,2,4,3), the option value can be save in this way: option -> 193929294939 and than you will know every value that the user has entered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM