Question: take the provided code and make a few changes (read all of them beforehand, otherwise you may make more work for yourself). When a user

take the provided code and make a few changes (read all of them beforehand, otherwise you may make more work for yourself).

When a user logs in: ask the user many values they want to support, enter this value into the log file

The program should then be able to support entering that many values (allocate enough memory to fit that amount)

If either by loading a file, or entering values through options two and three, if the number of read in values would exceed the maximum capacity, increase the capacity (without causing a memory leak) by 10 (so if the capacity is 20 and a 21st value is added then set the capacity to 30)

Whenever a storage increase takes place, log the old and new capacity.

Also: Your program may not use the '[' or ']' characters at any point. Or in other words: no arrays, use dynamic memory, keep in mind you cannot allow memory leaks in your code.

#include #include

#define MAX_NUMS 500 #define PASSWORDS_MISSING 1 #define STRING_SIZE 1024

int main() { int ints[MAX_NUMS]; char debug = 0; double sum; double doubles[MAX_NUMS]; int selection, icount = 0,dcount = 0; char user[STRING_SIZE], password[STRING_SIZE], curpass[STRING_SIZE], curuser[STRING_SIZE]; user[0] = 0; password[0] = 0; FILE *login, *log; char loggedIn; printf(" NOTE: You cannot run this sample program from the Shared folder You must run it from your own home folder "); log = fopen("hw4.log", "a"); if (log == 0) { printf("Could not open log file, exiting program "); return(1); } fprintf(log, "1234567: program started "); while(1) { loggedIn = 0; while(1) { printf("1) Log in 2) Exit "); scanf("%d", &selection); if (selection == 1) { printf(" User: "); scanf("%s", user); printf("Password: "); scanf("%s", password); login = fopen("passwords", "r"); if (login == 0) { printf("Could not open passwords file "); return(PASSWORDS_MISSING); } while(fscanf(login, "%s%s", curuser, curpass) != EOF) { if (strcmp(curuser, user) == 0 && strcmp(curpass, password) == 0) { loggedIn = 1; } } if (loggedIn == 0) { fprintf(log, "1234567: User %s login failed ", user); printf("Login failed "); } else { fprintf(log, "1234567: User %s login success ", user); printf("Logging in "); break; } for (selection = 0; selection < STRING_SIZE; selection++) { password[selection] = curpass[selection] = 0; } fclose(login); } else if (selection == 2) { printf("Exiting now "); fprintf(log, "1234567: program started "); fclose(log); return(0); } } while(1) { printf("Press 1 for Author name and ID Press 2 to enter floating point value Press 3 to enter integer value Press 4 to clear entered values Press 5 to print all values Press 6 to save data Press 7 to load data Press 0 to log out "); scanf("%d", &selection); if (selection == 1) { printf("Robert Fiske 1234567 "); } else if (selection == 0) { dcount = icount = 0; break; } else if (selection == 3) { if (icount == MAX_NUMS) { printf("You have reached the max, you must clear data before storing more. "); continue; } printf("Enter value "); scanf("%d", &ints[icount++]); } else if (selection == 2) { if (dcount == MAX_NUMS) { printf("You have reached the max, you must clear data before storing more. "); continue; } printf("Enter value "); scanf("%lf", &doubles[dcount++]); } else if (selection == 4) { icount = dcount = 0; } else if (selection == 5) { sum = 0; printf("FP Value: "); for (selection = 0; selection < dcount; selection++) { sum += doubles[selection]; printf("%lf ", doubles[selection]); } printf(" Int Value: "); for (selection = 0; selection < icount; selection++) { sum += ints[selection]; printf("%d ", ints[selection]); } printf(" Sum Value: %lf ", sum); } else if (selection == 6) { printf("Please enter file name "); scanf("%s", password); login = fopen(password, "w"); if (login == 0) { printf("Could not save file "); continue; } fprintf(login, "%s %d ", user, icount); for (selection = 0; selection < icount; selection++) { fprintf(login, "%d ", ints[selection]); } fprintf(login," %d ", dcount); for (selection = 0; selection < dcount; selection++) { fprintf(login, "%lf ", doubles[selection]); } fprintf(login, " "); fprintf(log, "1234567: Data saved user: %s file: %s ", user, password); fclose(login); } else if (selection == 7) { printf("Please enter file name "); scanf("%s", password); login = fopen(password, "r"); if (login == 0) { printf("Could not load file "); continue; } fscanf(login, "%s", curpass); if (strcmp(user, curpass) != 0) { printf("Save file is from a different user, cannot load "); fclose(login); continue; } fscanf(login, "%d", &icount); for (selection = 0; selection < icount; selection++) { fscanf(login, "%d", &ints[selection]); } fscanf(login, "%d", &dcount); for (selection = 0; selection < dcount; selection++) { fscanf(login, "%lf", &doubles[selection]); } fprintf(log, "1234567: Data loaded user: %s file: %s ", user, password); fclose(login); } else { printf("Invalid selection "); continue; } } } return(0); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!