Code:#include <stdio.h> #include <stdlib.h> #include <string.h> // number of salesman #define SIZE 100 // salesman_type structure definition typedef struct{ int ID; // salesman id char id_txt[6]; // salesman id for txt file char name[50]; // salesman name double hor_total; // total sales amount of a salesman }salesman_type; // end structure salesman_type salesman_type salesman[SIZE];// synonym for salesman_type // function prototype int nextId(int currentId, int isReset); int sale_menu(); void modifySales(); // function to generate id // currentId and isReset are parameters int nextId(int currentId, int isReset) { // initializes elements to 0 for the first time function is called static int lastId = 0; // if isReset is 0, assign currentId to lastId; if (isReset == 0) { lastId = currentId; } //if isReset is not 0, return and add 1 to lastId if (isReset != 0) return ++lastId; } // end function nextId // sales processing system menu function int sale_menu() { int menu; system("cls"); printf("Sales Processing System\n"); printf("-----------------------\n\n"); printf("1. Add Salesman Records\n"); printf("2. Reports Generation\n"); printf("3. Modify Salesman Records\n"); printf("4. Delete Salesman Records\n\n"); printf("0. Exit\n"); printf("Your choice: "); scanf("%d", &menu); return menu; printf("\n"); }// end function sale_menu void modifySales() { FILE *salesPtr; // sales.txt file pointer FILE *salesmanPtr; // salesman.txt file pointer FILE *tempSalesPtr; // temp.txt file pointer int a, b, modify; // counter double sale[SIZE][4]; // quartely sales amount // fopen opens the file; exits program if file cannot be opened if ( (salesPtr = fopen("sales.txt","r")) == NULL ) { printf("Cannot open sales.txt\n"); // display error message system("pause"); // pause program before it exits exit(-1); // end program } // end if else { // fopen opens the file; exits program if file cannot be opened if ( (salesmanPtr = fopen("salesman.txt","r")) == NULL) { printf("Cannot open salesman.txt"); // display error message system("pause"); // pause program before it exits exit(-1); // end program } // end if else { tempSalesPtr = fopen("temp.txt","w"); // prompt and read salesman id printf("\nSalesman ID (NO.): "); scanf("%d",&modify); for(a=0; a<SIZE; a++) { // read salesman id from file if there are still have salesman id if ((fscanf(salesPtr,"%[^|]|", salesman[a].id_txt) ) == 1 ) { fprintf(tempSalesPtr,"%s|",salesman[a].id_txt); // read salesman name //fscanf(salesmanPtr,"%*[^\n]\n%*s\t\t%[^\n]\n%*[^\n]\n%*[^\n]\n%*[^\n]\n\n",salesman[a].name); for (b=0; b<4; b++) { // read quarterly sales amount if(b<3) { if (a != modify - 1) { fscanf(salesPtr,"%lf|", &sale[a][b]); } else { printf("Quarter %d: ",b+1); scanf("%lf",&sale[a][b]); } fprintf(tempSalesPtr,"%.2f|",sale[a][b]); }// end if else { if (a != modify - 1) { fscanf(salesPtr,"%lf\n", &sale[a][b]); } else { printf("Quarter %d: ",b+1); scanf("%lf",&sale[a][b]); } fprintf(tempSalesPtr,"%.2f\n",sale[a][b]); } // end else } // end for } // end if } // end for } // end else fclose(salesPtr); // fclose close the file } // end else fclose(salesmanPtr); // fclose close the file fclose(tempSalesPtr); system("pause"); } // function main begins program execution int main() { // nextid.txt file pointer FILE *readIdPtr; // variable declaration int a, b, c = 0, last; // counter int getMenu = 10, getReport = 10; // menu selector double ver_total = 0, max = 0; int id = 0, lastId, resetId = 0; // fopen opens the file; exits program if file cannot be opened if ( (readIdPtr = fopen("nextid.txt","r") ) == NULL) { printf("Cannot open nextid.txt file\n"); system("pause"); exit(-1); } // end if else { // read lastId from file fscanf(readIdPtr,"%d",&lastId); // Generate new id from previous salesman ID if the salesman is one or more // lastId and restId are arguments nextId(lastId, resetId); } // end else // fclose close the file fclose(readIdPtr); printf("\n"); while (getMenu != 0) { // call and assign sale_menu function to getMenu // Execute command based on user input switch(getMenu = sale_menu()) { case 1: break; // call report_menu function case 2: break; case 3: modifySales(); break; } // end switch }// end while system("pause"); // pause the program before it exits return 0; } // end main
Why comes out the junk text?![]()



1Likes
LinkBack URL
About LinkBacks




