#include #include #include #include /* Declaring data structure */ struct inform
ID: 3656687 • Letter: #
Question
#include #include #include #include /* Declaring data structure */ struct information { int number; char name [25]; float balance; }; /* Function prototypes */ void safer_gets (char [], int); /* Safer gets function */ void safer_gets (char acct_name [], int max_sz) { /* Declare safer gets local variables */ int q; for (q = 0; q < max_sz - 1; q++) { acct_name [q] = getchar (); if (acct_name [q] == ' ') { acct_name [q] = ''; break; } /* End if statement */ } /* End for loop */ if (q == max_sz) if (acct_name [q - 1] != '') while (getchar () != ' '); acct_name [max_sz - 1] = ''; } /* End safer gets function */ /* Main program function */ main(void) { /* Declaring main local variables */ char bal_change = 'T'; char bal_valid = 'T'; char name_change = 'T'; char num_change = 'T'; char ok_to_proceed = 'T'; char sort_type; char test_bal [6]; int bal_ctr; int bal_valid_ctr; int data_struct_sz = 25; int export_file_ctr; int export_win_ctr; int field_width = 4; int input_ctr = 0; int name_array_sz = 25; int name_ctr; int num_ctr; int unique_ctr; struct information data [25]; struct information temp_sort[1]; /* Declaring string constants for report headers */ char header1[75] = " Client Number Client Name Balance"; char header2[75] = " ------------- ----------- -------"; /* Declaring main file pointer variable */ FILE *out_file_ptr; /* Display salutation */ printf (" Welcome to the Client Center. "); for(input_ctr = 0; input_ctr < data_struct_sz; input_ctr++) { /* Begin collecting account information */ do { ok_to_proceed = 'T'; /* Collect account member's number */ printf (" Enter Client Number (1 - 1000; -999 to end): "); fflush (stdin); scanf ("%i", &data[input_ctr].number); fflush (stdin); if (data[input_ctr].number != -999) { /* Validate the account numbers entered are within the acceptable range. If they are not within the acceptable range re-prompt. */ if (data[input_ctr].number < 1 || data[input_ctr].number > 1000) { printf (" *** Invalid entry. Account number must be greater "); printf ("than or equal to 1, less than or equal to 1000, or -999. "); printf ("to end. Please try again. *** "); ok_to_proceed = 'F'; } /* Ending if statement */ /* Validate the account numbers entered are unique. If they are not uniuque, re-prompt. */ for (unique_ctr = 0; unique_ctr < input_ctr; unique_ctr++) { if (data[input_ctr].number == data[unique_ctr].number) { printf(" *** Member account number already in use. Please "); printf("try again. *** "); ok_to_proceed = 'F'; } /* Ending if statement */ } /* Ending for loop */ } /* Ending if statement */ /* Validate the first account number entered is not the exit code. If it is the exit code, exit the program */ if (data[input_ctr].number == -999) { if (data[0].number == -999) { printf (" Thank you for using the Client Center. "); printf ("Have a nice day! "); return 1; } /* Ending if statement */ /* Validate the remaining numbers entered are not the exit code. If they are the exit code, exit the loop */ else { printf (" User has completed inputing data. Thank You. "); ok_to_proceed = 'T'; break; } /* Ending else statement */ } /* Ending if statement */ } while (ok_to_proceed == 'F'); /* Ending do-while loop */ /* Collect account member's name */ if (data[input_ctr].number != -999) { printf (" Enter full name for client number %i: ", data[input_ctr].number); /* Calling safer gets function to validate account member's name meets requirements for name array size */ fflush (stdin); safer_gets (data[input_ctr].name, name_array_sz); fflush (stdin); } /* Ending if statement */ /* Collect account member's balance */ if (data[input_ctr].number != -999) { do { do { printf (" Enter Balance amount for employee %s: ", data[input_ctr].name); fflush (stdin); scanf ("%5s", test_bal); fflush (stdin); bal_valid = 'T'; /* Validate user input is valid data */ for (bal_valid_ctr = 0; test_bal[bal_valid_ctr] != ''; bal_valid_ctr++) { if (!isdigit (test_bal[bal_valid_ctr])) { printf (" *** Illegal character entered. *** "); bal_valid = 'F'; break; } /* Ending if statement */ } /* Ending for loop */ } while (bal_valid == 'F'); /* Ending do-while loop */ /* Convert user input for account balance to a floating point value */ data[input_ctr].balance = atof (test_bal); /* Validate user input for account balance is a positive value */ if (data[input_ctr].balance <= 0) { printf (" *** Invalid entry. Account balance must be greater "); printf ("than 0. Please re-enter. *** "); } /* Ending if statement */ } while (data[input_ctr].balance <= 0); /* Ending do-while loop */ } /* Ending if statement */ } /* Ending for loop */ /* Display the number of accounts for which information has been collected */ if (data[input_ctr].number != -999) { printf (" Information collected for %i clients. ", input_ctr); } /* Ending if statement */ else { input_ctr = input_ctr - 1; printf (" Information collected for %i clients. ", input_ctr); } /* Ending else statement */ /* Collect the method by which account information should be sorted */ do { printf (" Sort by: Client Number (N), Name (A), or Balance (B): "); fflush (stdin); scanf ("%c", &sort_type); fflush (stdin); /* Sort by number */ if (toupper (sort_type) == 'N') { while (num_change == 'T') { num_change = 'F'; for (num_ctr = 0; num_ctr < input_ctr - 1; num_ctr++) { if (data[num_ctr].number > data[num_ctr + 1].number) { temp_sort [0] = data [num_ctr]; data [num_ctr] = data [num_ctr + 1]; data [num_ctr + 1] = temp_sort [0]; num_change = 'T'; } /* Ending if statement */ } /* Ending for loop */ } /* Ending while statement */ } /* Ending if statement */ /* Sort by name */ else if (toupper (sort_type) == 'A') { while (name_change == 'T') { name_change = 'F'; for (name_ctr = 0; name_ctr < input_ctr - 1; name_ctr++) { if (strcasecmp (data[name_ctr].name, data[name_ctr + 1].name) > 0) { temp_sort [0] = data [name_ctr]; data [name_ctr] = data [name_ctr + 1]; data [name_ctr + 1] = temp_sort [0]; name_change = 'T'; } /* Ending if statement */ } /* Ending for loop */ } /* Ending while statement */ } /* Ending else-if statement */ /* Sort by balance */ else if (toupper (sort_type) == 'B') { while (bal_change == 'T') { bal_change = 'F'; for (bal_ctr = 0; bal_ctr < input_ctr - 1; bal_ctr++) { if (data[bal_ctr].balance > data[bal_ctr + 1].balance) { temp_sort [0] = data [bal_ctr]; data [bal_ctr] = data [bal_ctr + 1]; data [bal_ctr + 1] = temp_sort [0]; bal_change = 'T'; } /* Ending if statement */ } /* Ending for loop */ } /* Ending while statement */ } /* Ending else-if statement */ else { printf (" *** Invalid entry. Sort criteria must be 'N' for "); printf ("Number, 'A' for Name, or 'B' for Balance. Please re-enter. *** "); } /* Ending else statement */ } while (toupper (sort_type) != 'N' && toupper (sort_type) != 'A' && toupper (sort_type) != 'B'); /* Ending do-while loop */ /* Display account information header to window */ printf (" "); printf ("%s ", header1); printf ("%s ", header2); printf (" "); /* Display account information data to window */ for (export_win_ctr = 0; export_win_ctr < input_ctr; export_win_ctr++) { printf (" "); printf ("%0*i", field_width, data[export_win_ctr].number); printf (" "); printf ("%-31s", data[export_win_ctr].name); printf ("%-.2f", data[export_win_ctr].balance); printf (" "); } /* Ending for loop */ /* Opening file and validating its existence */ out_file_ptr = fopen ("acct_info_#5", "w"); if (out_file_ptr == NULL) { printf ("Unable to open file acct_info_#5. Program terminating. "); return 1; } /* Ending if statement */ /* Display account information header to acct_info_#5 file */ fprintf (out_file_ptr, " "); fprintf (out_file_ptr, "%s ", header1); fprintf (out_file_ptr, "%s ", header2); fprintf (out_file_ptr, " "); /* Display account information data to acct_info_#5 file */ for (export_file_ctr = 0; export_file_ctr < input_ctr; export_file_ctr++) { fprintf (out_file_ptr, " "); fprintf (out_file_ptr, "%0*i", field_width, data[export_file_ctr].number); fprintf (out_file_ptr, " "); fprintf (out_file_ptr, "%-31s", data[export_file_ctr].name); fprintf (out_file_ptr, "%.2f", data[export_file_ctr].balance); fprintf (out_file_ptr, " "); } /* Ending for loop */ /* Closing file */ fclose (out_file_ptr); getchar (); } /* Ending main program function */Explanation / Answer
if (data[input_ctr].number == -999) //input check { if (data[0].number == -999) // data check { printf (" Thank you for using the Client Center. "); printf ("Have a nice day! "); return 1; } /* Ending if statement */ Note according to your code if both the condition satisfy only then return is executed write it as if (data[input_ctr].number == -999) { printf (" Thank you for using the Client Center. "); printf ("Have a nice day! "); return 1; } else if (data[0].number == -999) // data check { printf (" Thank you for using the Client Center. "); printf ("Have a nice day! "); return 1; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.