Thread: Multiple Printing?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    Multiple Printing?

    Hi, In case 3, when I type the wrong ID i get the error messaged printed 4 times, when I get is right it prints depending on the position of the value thats stored in the array.
    ex.
    ---=== EMPLOYEE DATA ===---




    1. Display Employee Information
    2. Add Employee
    3. Update Employee Salary
    4. Remove Employee
    0. Exit


    Please select from the above options: 2


    Adding Employee
    ===============
    Enter Employee ID: 222
    Enter Employee Age: 22
    Enter Employee Salary: 2


    1. Display Employee Information
    2. Add Employee
    3. Update Employee Salary
    4. Remove Employee
    0. Exit


    Please select from the above options: 2


    Adding Employee
    ===============
    Enter Employee ID: 333
    Enter Employee Age: 3
    Enter Employee Salary: 3


    1. Display Employee Information
    2. Add Employee
    3. Update Employee Salary
    4. Remove Employee
    0. Exit


    Please select from the above options: 2


    Adding Employee
    ===============
    Enter Employee ID: 444
    Enter Employee Age: 4
    Enter Employee Salary: 4


    1. Display Employee Information
    2. Add Employee
    3. Update Employee Salary
    4. Remove Employee
    0. Exit


    Please select from the above options: 2


    Adding Employee
    ===============
    Enter Employee ID: 555
    Enter Employee Age: 5
    Enter Employee Salary: 5


    1. Display Employee Information
    2. Add Employee
    3. Update Employee Salary
    4. Remove Employee
    0. Exit


    Please select from the above options: 3


    Update Employee Salary
    ===============
    Enter Employee ID: 123
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***
    Enter Employee ID: 321
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***
    Enter Employee ID: 333
    *** ERROR: Employee ID not found! ***
    The current salary is $3.00
    Enter New Employee Salary:123
    *** ERROR: Employee ID not found! ***
    *** ERROR: Employee ID not found! ***

    Here is my code atm
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #define SIZE 4
    
    
    #include <stdio.h>
    struct employee {
    	int id;
    	int age;
    	double salary;
    };
    
    
    
    
    int main(void)
    {
    
    
    	int option = 0, i = 0, j = 0, n = 0, flag = 0;
    	
    	struct employee emp[SIZE] = {{0}};
    
    
    
    
    	printf("---=== EMPLOYEE DATA ===---\n\n");
    
    
    	do {
    		printf("\n");
    		printf("1. Display Employee Information\n");
    		printf("2. Add Employee\n");
    		printf("3. Update Employee Salary\n");
    		printf("4. Remove Employee\n");
    		printf("0. Exit\n\n");
    		printf("Please select from the above options: ");
    
    
    		scanf("%d", &option);
    		printf("\n");
    
    
    		switch (option) {
    		case 0:
    			printf("Exiting Employee Data Program. Good Bye!!!\n");
    			printf("\n");
    
    
    			break;
    		case 1:
    
    
    			
    			printf("EMP ID  EMP AGE EMP SALARY\n");
    			printf("======  ======= ==========\n");
    			for (i = 0; i < SIZE; i++) {
    
    
    				if (emp[i].id > 0) {
    					printf("%6d%9d%11.2lf", emp[i].id, emp[i].age, emp[i].salary);
    					printf("\n");
    					
    				}
    				
    			}
    			break;
    		case 2:	
    
    
    
    
    			printf("Adding Employee\n");
    			printf("===============\n");
    
    
    			if (n < SIZE){
    			printf("Enter Employee ID: ");
    			scanf("%d", &emp[n].id);
    			printf("Enter Employee Age: ");
    			scanf("%d", &emp[n].age);
    			printf("Enter Employee Salary: ");
    			scanf("%lf", &emp[n].salary);
    			n++;
    			
    		} else 
    				printf("ERROR!!! Maximum Number of Employees Reached\n");
    			
    
    
    			break;
    
    
    
    
    		case 3:
    			printf("Update Employee Salary\n");
    			printf("===============\n");
    			do {
    				printf("Enter Employee ID: ");
    				scanf(" %d", &j);
    
    
    				for (i = 0; i < SIZE; i++) {
    
    
    					if (j == emp[i].id) {
    
    
    						printf("The current salary is $%0.2lf", emp[i].salary);
    						printf("\nEnter New Employee Salary:");
    						scanf(" %11lf", &emp[i].salary);
    						flag = 1;
    					} 
    
    
    
    
    
    
    					if (j != emp[i].id) {
    							printf("*** ERROR: Employee ID not found! ***");
    							printf("\n");
    
    
    					}
    
    
    					
    				}
    			
    				
    			} while (flag == 0);
    			
    			
    			break;
    
    
    		case 4:
    			
    
    
    			break;
    		default:
    			printf("ERROR: Incorrect Option: Try Again\n");
    		}
    
    
    	} while (option != 0);
    
    
    
    
    	return 0;
    }

    Any help will be appreciated/

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Hi, In case 3, when I type the wrong ID i get the error messaged printed 4 times, when I get is right it prints depending on the position of the value thats stored in the array.
    I'd recommend you try to figure out why when you enter a "correct" ID it prints the error message multiple times. If you fix this it may just fix the other problem. Hint: What is that for() loop doing?

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    4
    Thanks, Got the printf issue done. Now my instructions were to copy the do-while for case 4, after the first incorrect it will just end and bring up the menu. If I do case 4 then case 3, Case 3 will have this issue.

    Code:
    		case 3:
    			printf("Update Employee Salary\n");
    			printf("===============\n");
    			do {
    				printf("Enter Employee ID: ");
    				scanf(" %d", &j);
    
    
    				
    				for (i = 0; i < SIZE; i++) {
    					if (j == emp[i].id)
    					{
    
    
    						printf("The current salary is %0.2lf", emp[i].salary);
    						printf("\nEnter New Employee Salary: ");
    						scanf(" %11lf", &emp[i].salary);
    						flag = 1;
    						break;
    
    
    					}
    				}
    
    
    
    
    
    
    
    
    					if (j != emp[i].id) {
    						printf("*** ERROR: Employee ID not found! ***");
    						printf("\n");
    						
    
    
    					}
    			} while (flag == 0);
    
    
    			
    			
    			break;
    
    
    		case 4:
    			printf("Remove Employee\n");
    			printf("===============\n");
    			do {
    				printf("Enter Employee ID: ");
    				scanf(" %d", &j);
    
    
    
    
    				for (i = 0; i < SIZE; i++) {
    					if (j == emp[i].id)
    					{
    						printf("Employee %d will be removed\n", emp[i].id);
    						emp[i].id = 0;
    						flag = 1;
    						break;	
    					}
    					
    				}
    					
    					if (j != emp[i].id) {
    						printf("*** ERROR: Employee ID not found! ***");
    						printf("\n");
    					}
    			} while (flag == 0);
    
    
    			
    			break;
    
    
    
    
    		default:
    			printf("ERROR: Incorrect Option: Try Again\n");
    		}
    
    
    	} while (option != 0);
    
    
    
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2014, 05:46 PM
  2. Replies: 2
    Last Post: 03-21-2014, 04:14 PM
  3. Printing To Multiple Files
    By hasush in forum C Programming
    Replies: 2
    Last Post: 04-18-2012, 02:54 PM
  4. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

Tags for this Thread