Question

Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");...

Explain this code function by function in detail

int menu()

{

  int choice;

  do {

    system("cls");

    printf("1-Insurence Plan Subscription\n");

    printf("2-Claim Processing\n");

    printf("3-Accounts Information\n");

    printf("4-Searching Functionalities\n");

    printf("5-Exit\n");

    scanf("%d", &choice);

  } while (choice > 5 || choice < 1);

  return choice;

void subscribe()

system("cls");

  victims[userCount].id = userCount + 1;

  printf("Enter age: ");

  scanf("%d", &victims[userCount].age);

  printf("\n\n%-25sHealth Insurence Plan\n\n", " ");

  printf("-----------------------------------------------------------------------------------------------\n");

  printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");

  printf("-----------------------------------------------------------------------------------------------\n");

  printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);

  printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000,150000,200000);

  printf("|%-30s|%-20d|%-20d|%-20d|\n", "Lifetime Claim Limit", 600000,750000,1000000);

  printf("\n\n%-25sAge Group and Health Insurence Plan\n\n", " ");

  printf("|%-30s|%-25s%-30s\n", "Types of Claim"," ","Eligibility Amount");

  printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");

  printf("|%-30s|%-20s|%-20s|%-20s|\n", "Room Charges", "120/day", "150/day", "200/day");

  printf("|%-30s|%-20s|%-20s|%-20s|\n", "Intensive Care Unit", "250/day", "400/day", "700/day");

  printf("|%-30s|%-20s|%-20s|%-20s|\n", "Hospital Supplies and Services", " "," "," ");

  printf("|%-30s|%-60s|\n", "Surgical Fees", "As charged Sbject to approval by ZeeMediLife");

  printf("|%-30s|%-20s|%-20s|%-20s|\n", "Other Fees", " ", " ", " ");

  int choice;

  do{

    printf("Select a Claim Limit Type\n1-Annual Claim Limit 2-Lifetime Claim Limit: ");

    scanf("%d", &choice);

  } while (choice < 1 || choice>2);

  if (choice == 1)

    victims[userCount].annualClaim = true;

  else

    victims[userCount].annualClaim = false;

  do {

    printf("Select a plan\n1-Plan120 2-Plan150 3-Plan200: ");

    scanf("%d", &choice);

  } while (choice < 1 || choice>3);

  victims[userCount].plan = choice;

  printf("Enter Name: ");

  scanf("%s", &victims[userCount].name);

  printf("Contact Number: ");

  scanf("%s", &victims[userCount].contactNum);

  printf("Enter Address: ");

  scanf("%s", &victims[userCount].address);

  FILE *fp;

  fp = fopen("users.txt", "a");

  fprintf(fp, "%d,%s,%d,%d,%d,%s,%s\n", victims[userCount].id, victims[userCount].name, victims[userCount].age, victims[userCount].annualClaim, victims[userCount].plan, victims[userCount].contactNum, victims[userCount].address);

  fclose(fp);

  time_t s;

  struct tm* currentTime;

  s = time(NULL);

  currentTime = localtime(&s);

  claims[userCount].id = victims[userCount].id;

  claims[userCount].amountClaimed = 0;

  claims[userCount].claimedYear = currentTime->tm_hour + 1900;

  if (victims[userCount].annualClaim == true)

    if (victims[userCount].plan == 1)

      claims[userCount].remaininigAmount = 120000;

    else if (victims[userCount].plan == 2)

      claims[userCount].remaininigAmount = 150000;

    else

      claims[userCount].remaininigAmount = 200000;

  }  else {

    if (victims[userCount].plan == 1)

      claims[userCount].remaininigAmount = 600000;

    else if (victims[userCount].plan == 2)

      claims[userCount].remaininigAmount = 750000;

    else

      claims[userCount].remaininigAmount = 1000000;

  }userCount++;

  fp = fopen("claims.txt", "a");

  if (fp == NULL)

    printf("File Dont exist");

  fprintf(fp,"%d,%d,%d,%d\n", claims[userCount].id, claims[userCount].claimedYear, claims[userCount].amountClaimed, claims[userCount].remaininigAmount);

  fclose(fp);

  system("pause");

void claimProcess()

{  int id;

  bool found = false;

  system("cls");

    printf("Enter user ID for which you want to claim insurrence: ");

  scanf("%d", &id);  int i;

  for (i = 0; i < userCount; i++)

  {

    if (victims[i].id == id)

    {

      found = true;

      break;

    }

  }

  if (found == false)

  {

    printf("subscriber not found\n");

    return;

  }

  int numOfDaysHospitalized, suppliesCost, surgicalFee, otherCharges;

  bool ICU;

  printf("How many days were you haspitalized: ");

  scanf("%d", &numOfDaysHospitalized);

  int ICUFlag;

  do {

    printf("Select A Ward Type\n1-Normal Ward 2-ICU: ");

    scanf("%d", &ICUFlag);

  } while (ICUFlag < 1 || ICUFlag>2);

  if (ICUFlag == 2)

    ICU = true;

  else

    ICU = false;

  printf("Enter Cost of Supplies and Services: ");

  scanf("%d", &suppliesCost);

  printf("Enter Surgical Fees: ");

  scanf("%d", &surgicalFee);

  printf("Enter Other Charges: ");

  scanf("%d", &otherCharges);

  int ICUCharges = 0;

  if (ICU == true)

  {

    if (victims[i].plan == 1)

      ICUCharges = numOfDaysHospitalized * 120;

    else if (victims[i].plan == 2)

      ICUCharges = numOfDaysHospitalized * 150;

    else

      ICUCharges = numOfDaysHospitalized * 200;

  }

  else

  {

    if (victims[i].plan == 1)

      ICUCharges = numOfDaysHospitalized * 250;

    else if (victims[i].plan == 2)

      ICUCharges = numOfDaysHospitalized * 400;

    else

      ICUCharges = numOfDaysHospitalized * 700;

  }

  int totalClaimAmount = numOfDaysHospitalized + suppliesCost + surgicalFee + otherCharges + ICUCharges;

    if (victims[i].annualClaim == true)

  {

    if (victims[i].age > 60)

    {

      printf("The subscriber age has exceeded the limit(60 Year), Not Eligible");

      return;

    }

  }

  int j;

  for (j = 0; j < userCount; j++)

  {

    if (claims[j].id == victims[i].id)

      break;

  }

  int amountToBeBorne = 0;

  if (totalClaimAmount <= claims[j].remaininigAmount)

  {

    claims[j].amountClaimed += totalClaimAmount;

    claims[j].remaininigAmount -= totalClaimAmount;

  }

  else

  {

    amountToBeBorne = totalClaimAmount - claims[j].remaininigAmount;

    claims[j].amountClaimed += (totalClaimAmount - amountToBeBorne);

    claims[j].remaininigAmount = 0;

    printf("\n\nThe amount that can be claimed is less then the claim by subscriber. %d RM should be given by subscriber themselves\n", amountToBeBorne);

  }

  FILE *fp;

  fp = fopen("claims.txt", "w");

  for(int i=0;i<userCount;i++)

    fprintf(fp,"%d,%d,%d,%d\n", claims[i].id, claims[i].claimedYear, claims[i].amountClaimed, claims[i].remaininigAmount);

  fclose(fp);

  printf("Subscriber ID: %d\nSubscriber Name: %s\nSubscriber Claimed Year: %d\nSubscriber amount Claimed: %d\nSubscriber Remaining Claimed: %d\n", claims[j].id, victims[i].name, claims[j].claimedYear, claims[j].amountClaimed, claims[j].remaininigAmount);

  system("pause");

}

void accountInfo()

{

  system("cls");

  int choice;

  printf("1-Total Amount Claimed by LifeTime Claim Limit subscriber\n");

  printf("2-Total number of Annual Claim Limit who have exhausted all their eligible amount\n");

  scanf("%d", &choice);

    if (choice == 1)

  {

    int totalAmountClaimedByLifeTimeSubs = 0;

    for (int i = 0; i < userCount; i++)

    {

      if (victims[i].annualClaim == false)

      {

        for (int j = 0; j < userCount; j++)

        {

          if (claims[j].id == victims[i].id)

          {

            totalAmountClaimedByLifeTimeSubs += claims[j].amountClaimed;

          }

    printf("\nTotal amount Claimed By LifeTime Subscribers is: %d\n", totalAmountClaimedByLifeTimeSubs);

  }

  else

  {

    int count = 0;

    for (int i = 0; i < userCount; i++)

    {

      if (claims[i].remaininigAmount <= 0 && victims[i].annualClaim == true)

        count++;

    }

    printf("Total number of Annual Claim Limit Subcriber who have exhausted all their amount are: %d\n", count);

  }

  system("pause");

}

void searchingFunctionalities()

{

  system("cls");

  int choice;

  printf("1-Search by ID\n2-Search by age\n");

  scanf("%d", &choice);

  if (choice == 1)

  {

    int id;

    printf("Enter user ID for which you want Search: ");

    scanf("%d", &id);

    int i;

    for (i = 0; i < userCount; i++)

    {

      if (victims[i].id == id)

      {

        printf("\nSubscriber Name: %s\nSubscriber Age: %d\nSubscriber Contact: %s\nSubscriber Address: %s\n", victims[i].name, victims[i].age, victims[i].contactNum, victims[i].address);

        break;

      }

    }

    printf("Subscriber Not Found");

  }

  else

  {

    int age;

    printf("Enter age for which you want Search: ");

    scanf("%d", &age);

    for (int i = 0; i < userCount; i++)

    {

      if (victims[i].age == age)

      {

        printf("\nSubscriber Name: %s\nSubscriber ID: %d\nSubscriber Contact: %s\nSubscriber Address: %s\n", victims[i].name, victims[i].id, victims[i].contactNum, victims[i].address);

      }

  system("pause");

}

int main()

{

  int choice;

  loadData();

  do {

    choice = menu();

    if (choice == 1)

      subscribe();

    else if (choice == 2)

      claimProcess();

    else if (choice == 3)

      accountInfo();

    else if (choice == 4)

      searchingFunctionalities();

    else

      break;

  } while (choice != 5);

  system("pause");

  return 0;

}

Homework Answers

Answer #1

Explanation:

  • The C++ Program execution start from executing the preprocessor statements i.e, the statements starting from '#"
  • Firstly the header files which are stdio.h, stdlib.h, stdbool.h, string.h and time.h are included and MAX_LENGTH is defined as 500.
  • Then the Program execution goes to the main function and starts the execution

Main Functon:

  • In the main function an integer variable choice is declared for the selection of the choice of the user from the menu.
  • Then the function loadData() is called for the initialization of the user structure and the claim structure from their respective .txt files.

loadData():

  • In loadData() function firstly a character's array line is declared of max_length to read the data from the file line by line.
  • Then a delimitter is declared with a null charater.
  • *fp is file pointer using which the file users.txt is opened in read mode (r)
  • c is for the charater-reading.
  • When fp is NULL it implies that the file mentioned in fopen is not found.
  • Then it checks whether the file contains any data or it is empty by the function fgets which used to read data line by line from the file.
  • if the conditions are true i.e the file is empty it breaks the while loop and the file get closed with fclose()
  • If conditions are false then the memebers of the users structure are initialized with the null values.
  • atoi() is function which converts string to integer.
  • strtok() is string function to get a string token.
  • usercount is incremented, and the file users.txt is closed
  • Similarly the execution goes for the claims part

Then in the main function

the menu() function is called. The menu() function prints the menu of the application through printf() then it reads and integer value as choice through scanf() and returns it to the choice variable of the main() function.

NOTE: The choice variable of the main() function and the menu() function are different variables and they have different addresses and scope.

if choice is equal to 1 the subscribe() function is called.

subscribe():

  • Firstly the user id is incremented and initialized to the user[userCount].id as a new subscription is getting started.
  • Then it reads the age of the user and strores in the structure users.
  • Then it prints the plans of the company providing through printf().
  • The program now reads the choices of the user as mentioned in the plans through scanf() and assigns it to the respective data member of users structure.
  • Then through the file pointer fp it opens the file users in append mode so that the details of the subscriber could be added at the end of the file.
  • Then fprintf() function is called which prints the values in the file and the file is closed.
  • Then time variable is declared to access the time from the system as well as the current time.
  • according to the plans as mentioned above in the initial part of the subscribe() function the respective assignment of the data to the data members of the claims structure is done .
  • Then the claims.txt file is opened in the append mode using fopen() function with the fp file pointer
  • Then through fprintf() the mentioned data memebers of the structure claims is printed to the claims.txt file and the file is closed.

If choice is equal to 2 then claimProcess() function is called.

claimProcess():

  • In the claimProcess() function an integer variable id is declared to read the id of the claiming subscriber.
  • found is a bool variable which is holds the value whether the subscriber id exists true or false and is initialized as false.
  • Using the for all the user's id are matched with the id given by the claiming subscriber.
  • through if ( user[id].id == id)
  • if the condition is true the found will be assigned as true, else by default the found is initialized as false.
  • if found is false it prints the error message.
  • if found is true it breaks the for loop and proceeds to further lines of code.
  • The variables numOfDaysHospiatalized, suppliesCost, surgicalFee, otherCharges are declared as int and then these values are read from the claiming subscriber for further calculation.
  • ICUFLag is used to confim the type of ward whether it is a normal-ward of ICU.
  • Then according to the plans as mentioned above in the program the respective calculation is done.
  • with the nested if else statements and the arithmetic operations.
  • The claims.txt file is opened in the write mode and through fprintf() new values of claims structure are written to the claims.txt file using for loop.
  • And the current values of that particular claiming subscriber is displayed through printf().

If choice is equal to 3 in the main() function the accountInfo() is called:

  • accountInfo():
  • The menu is printed with the two options mentioned, and the choice from the user is read and assigned to the integer variable choice.
  • if choice is equal to 1
  • The variable totalAmountClaimedByLifeTimeSubs is initialized to 0.
  • Using for loop it iterates all the users, it checks the conditioned whether the user's annualClaim is false, if it is true then all the claimedAmount-s of the user are cumulatively added to the totalAmountClaimedByLifeTimeSubs.
  • And then it prints the totalAmountClaimedByLifeTimeSubs
  • if choice is equal to 2
  • count is an integer variable initilaized as which refers to the number of annual claim limit subscriber who have exhausted all their amount.
  • Using for loop it iterates all the user and it checks that whether the remainingAmount is less than or equal to 0 (<= )
  • and (&&) also the annualClaim of the current user is true, then the count is incremented.
  • And when all the users are iterated the number of annual claim limit subscriber who have exhausted all their amount are displayed.

if the choice is equal to 4 in the main() function searchingFunctionalities() is called:

  • It provides two options whether to search by ID or search by age. then from the user the value of choice is read.
  • if choice is equal to 1
  • It reads the ID from the user and checks through the for loop, if the users[i].id is equal to the user given id then
  • it prints the particular data members of that instance of the users structure.
  • if the id unmatched with any user's id then it prints the error message
  • if choice is equal to 2
  • The process is similar to that of id, instead of id it is used
  • users[i].age is equal to the user given age then
  • it prints the particular data members of that instance of the users structure.
  • In the main() function :
  • if the choice is not in [1,2,3,4] then it breaks the do-while loop and the main function waits for key press and with a return value 0 the execution of program ends.

**Fell free to ask any queries in the comment section. I am happy to help you. if you like our work, please give Thumbs up**

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design flow chart on software not handwritten. void claimProcess() {    int id;    bool found...
Design flow chart on software not handwritten. void claimProcess() {    int id;    bool found = false;    system("cls");       printf("Enter user ID for which you want to claim insurrence: ");    scanf("%d", &id);    int i;    for (i = 0; i < userCount; i++)    {        if (users[i].id == id)        {            found = true;            break;        }    }    if (found == false)   ...
Design flow chart of this code and make sure it is not handwritten it must on...
Design flow chart of this code and make sure it is not handwritten it must on some software. struct user {    int id;    int age;    bool annualClaim;    int plan;    char name[30];    char contactNum[15];    char address[50]; }; #define MAX_LENGTH 500 struct user users[100]; int userCount = 0; struct claim {    int id;    int claimedYear;    int amountClaimed;    int remaininigAmount; }; struct claim claims[100]; void loadData() {    char line[MAX_LENGTH];    const...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...