Thread: Need Urgent Help!!!

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    1

    Need Urgent Help!!!

    Im about to present this program in a few hours but my brain is only capable of this. Need your help friends.

    The program is a record system that allows the user to input the details of employees their name, position and salary. The program needs to have and add, search, sort and delete and should have 3 structures. Im just a beginner i need all the help i can get.

    Code:
    #include<stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #define size 50
    
    
    typedef struct {
    	char fullname[30];
    	char lname[30];
    	char position[20];
    	char id[3];
    	float salary;
    }employee;
    
    
    void menu(){
    	printf("\n\t\n\tM E N U");
    	printf("\n\n[1] Add New Employee");
    	printf("\n[2] Increase Salary of an Employee");
    	printf("\n[3] Decrease Salary of an Employee");
    	printf("\n[4] Display an Employee Info");
    	printf("\n[5] Display All Employee");
    	printf("\n[0] Exit");
    }
    
    
    void displayone(employee e[],int max){
    	char search1[size];
    	int i;
    	system("cls");
    	
    	do{
    		printf("\nInput Employee ID#: ");
    		gets(search1);
    		fflush(stdin);
    		for(i=0;i<size;i++){
    			if(strcmpi(search1,e[i].id)==0){
    				printf("\nID: %s", e[i].id);
    				printf("\nFullname: %s",e[i].fullname);
    				printf("\nPosition: %s", e[i].position);
    				printf("\nSalary: %.2f", e[i].salary);
    				getch();
    				return;
    			}
    		}
    		printf("\nRecord not Found");
    	}while(1);
    }
    
    
    void displayAll(employee e[],int max){
    char tempname[size],tempid[size], temppos[size];
    float tempbal;
    int a,b,c;
    	
    for (b = 0; b < max - 1 ; b++)
       	{
        for (c = b + 1; c < max; c++)
          	{
              	if (strcmp(e[b].fullname, e[c].fullname) > 0)
               	{
    			strcpy(tempname,e[b].id);
    			strcpy(tempid,e[b].position);
    			strcpy(temppos,e[b].fullname);
    			tempbal=e[b].salary;
    											
    			strcpy(e[b].id,e[c].id);
    			strcpy(e[b].position,e[c].position);
    			strcpy(e[b].fullname,e[c].fullname);
    			e[b].salary=e[c].salary;
    						
    			strcpy(e[c].fullname,tempname);
    			strcpy(e[c].id,tempid);
    			strcpy(e[c].position,temppos);
    			e[c].salary = tempbal;
                }
           	}
      	}
    		int i;
    		
      		system("cls");
      		printf("Employee\tID #\tName\t\tPosition\tSalary\n\n");
      		for(i=0;i<max;i++)
    		{
    			printf("%d\t\t%s\t%s\t\t%s\t\t%.2f\n",i+1,e[i].id,e[i].fullname,e[i].position,e[i].salary);	
    		}
    		printf("\n\n\t");
    		system("pause");
    }
    
    
    void addsalary(employee e[],int max){
    	char search2[size];
    	int i;
    	float newsal;
    	system("cls");
    	
    	do{	
    		printf("\nIncrease Salary of Employee");
    		printf("\nInput Employee ID: ");
    		gets(search2);
    		fflush(stdin);
    		for(i=0;i<max;i++){
    			if(strcmpi(search2,e[i].id)==0){
    				printf("\nID: %s", e[i].id);
    				printf("\nName: %s",e[i].fullname);
    				printf("\nPosition: %s",e[i].position);
    				printf("\nSalary: %.2f\n", e[i].salary);
    				printf("\nHow much would you like to add?: ");
    				scanf("%f", &newsal);
    				e[i].salary += newsal;
    				printf("\nSuccessfully Updated Salary to %.2f ", e[i].salary);
    				getch();
    				return;
    			}
    		}
    		printf("\nRecord not Found! Please try again!\n");
    	}while(1);
    }
    
    
    void desalary(employee e[],int max){
    	char search2[size];
    	int i;
    	float newsal;
    	system("cls");
    	
    	do{	
    		printf("\nDecrease Salary of Employee");
    		printf("\nInput Employee ID: ");
    		gets(search2);
    		fflush(stdin);
    		for(i=0;i<max;i++){
    			if(strcmpi(search2,e[i].id)==0){
    				printf("\nID: %s", e[i].id);
    				printf("\nName: %s",e[i].fullname);
    				printf("\nPosition: %s",e[i].position);
    				printf("\nSalary: %.2f\n", e[i].salary);
    				printf("\nHow much would you like to decrease?: ");
    				scanf("%f", &newsal);
    				e[i].salary -= newsal;
    				printf("\nSuccessfully Updated Salary to %.2f ", e[i].salary);
    				getch();
    				return;
    			}
    		}
    		printf("\nRecord not Found! Please try again!\n");
    	}while(1);
    }
    
    
    void idnum(employee e[], int a){
    	int b;
    	printf("\nID #: ");
    	fflush(stdin);
    	gets(e[a].id);
    	for(b=0;b<a;b++)
    	{
    		if(strcmpi(e[a].id,e[b].id)==0){
    			printf("This ID number is already in use!\n");
    			idnum(e,a);
    		}
    	}
    }
    
    
    void fname(employee e[], int a){
    	printf("\nFull name: ");fflush(stdin);gets(e[a].fullname);
    }
    
    
    void positions(employee e[],int a){
    	printf("\nPosition: ");fflush(stdin);gets(e[a].position);
    }
    
    
    void salaryamt(employee e[],int a){
    	printf("\nSalary: ");fflush(stdin);scanf("%f", &e[a].salary);
    }
    
    
    void add(employee e[],int max){	
    	int a;
    	for(a=max-1;a<max;a++){
    		idnum(e,a);
    		fname(e,a);
    		positions(e,a);
    		salaryamt(e,a);
    		printf("\n");
    	}
    	
    }
    
    
    main(){
    	int choice,max=1,a;
    	employee e[size];
    	for(a=0;a<max;a++){
    		printf("\nEmployee %d\n\n", a+1);
    		idnum(e,a);
    		fname(e,a);
    		positions(e,a);
    		salaryamt(e,a);
    		printf("\n");
    	}
    	system("cls");
    	
    	do{
    		do{
    			menu();
    	printf("\n\nWhat do you want to do? "); scanf("%d", &choice); system("cls");
    			if(choice <=0 || choice > 5){
    					printf("\nInvalid Choice! (1-5 only). Choose Again... \n\n");
    				}
    			}while(choice <=0 || choice > 5);
    		
    		fflush(stdin);
    		switch(choice)
    				{
    		case 1: 		system("cls");
    						max++;
    						add(e,max);
    						
    						system("cls");
    						break;
    					
    		case 2:			system("cls");
    						addsalary(e,max);
    						system("cls");
    						break;
    						
    		case 3:			system("cls");
    						desalary(e,max);
    						system("cls");
    						break;				
    						
    		case 4: 		system("cls");
    						displayone(e,max);
    						system("cls");
    						break;
    			
    		case 5: 		system("cls");
    						displayAll(e,max);
    						system("cls");
    						break;
    							
    			}
    	}while(choice != 0);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    So what seems to be the problem?

    Edit: Also you should never, Never, NEVER use gets(). This function is so unsafe that it has been removed from the current C standard.
    Last edited by jimblumberg; 10-09-2019 at 01:04 PM.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Urgent?! Who's dying?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Two other problems in addition to @jimblumberg's comments.

    fflush(stdin) should NEVER be used! fflush() is for output file only! fflush(stdin) is a perfect example of Undefined Behavior!

    Your code formatting should be cleaned up. Please see the Wikipedia article that lists several different formatting styles. Choose one and please be consistent.

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Code:
    case 4:         system("cls");
                            displayone(e,max);
                            system("cls");
                            break;
                 
            case 5:         system("cls");
                            displayAll(e,max);
                            system("cls");
                            break;
    What's the point of displaying the details only to immediately clear them from the screen before the user even has the chance to read them?

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Please, stop using system() in your code...

    1. "cls" is available in Windows/DOS only;
    2. it isn't safe... What if you have an "CLS.COM or CLS.EXE" in your PATH?
    3. It is SLOW!

    Here's a better routine to clear your screen in a terminal:

    Code:
    #if defined(__WINNT)
    #include <windows.h>
      
    /* Uses Win32 Console API */
    void clearscreen(void)
    {
      HANDLE hConsole;
      COORD coord = {0, 0};
      CONSOLE_SCREEN_BUFFER_INFO csbi;
      DWORD dwConsoleSize, dwWriten;
      
      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      GetConsoleScreenBufferInfo(hConsole, &csbi);
      dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y;
      
      FillConsoleOutputCharacter(hConsole, (TCHAR)' ', 
                                 dwConsoleSize, coord, &dwWriten);
      FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                 dwConsoleSize, coord, &dwWriten);
      
      SetConsoleCursorPosition(hConsole, coord);
    }
    #else
    #include <stdio.h>
      
    /* Uses ANSI -- almost all Unix terminals supports it. */
    void clearscreen(void) { fputs("\x1b[2J", stdout); }
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent Help plz....
    By shansajid in forum C Programming
    Replies: 3
    Last Post: 01-05-2013, 05:44 AM
  2. need urgent help
    By luke luvevou in forum C Programming
    Replies: 14
    Last Post: 06-10-2009, 07:18 PM
  3. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  4. Please help me (urgent)
    By forfor in forum C Programming
    Replies: 3
    Last Post: 12-05-2002, 03:32 PM

Tags for this Thread