Thread: Searching ID number

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    12

    Searching ID number

    Hi guys... Good Day!

    I'm having a problem with my code below.

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
     {
      struct Payroll_System
         {
          char fname[40], sname[40], mi[2];
          float id, bpay;
          }
          emp[5];
          int z=0 , x, y;
    
          char sel;
          char choice;
          clrscr();
          do
             {
              printf("PAYROLL SYSTEM\n\n");
              printf("MENU:\n 1. Enter Employees Record\n 2. Process Payroll\n 3. Payroll Summary\n 4. Employee Payslip\n 5. Exit\n");
              printf("Enter your choice: ");
              choice=getche();
              switch(choice)
                 {
                  case '1':
                   do
                        {
                         clrscr();
                         z++;
                         printf("Write Record \n\n\n");
                         printf("Employee Number: ");
                         scanf("%i", &emp[z].id);
                         printf("Employee Name\n");
                         printf("First Name: ");
                         scanf("%s", &emp[z].fname);
                         printf("Middle Initial: ");
                         scanf("%s", &emp[z].mi);
                         printf("Last Name: ");
                         scanf("%s", &emp[z].sname);
                         printf("Basic Pay (Daily): ");
                         scanf("%i", &emp[z].bpay);
    	             clrscr();
                         printf("Employee: %s, %s %s \nSuccessfully Added!\n\n\n", emp[z].sname, emp[z].fname, emp[z].mi);
                         printf("Add New Record (y/n): ");
                         scanf("%s" , &sel);
                         clrscr();
                        }
                        while(sel != 'n');
                       break;
                 case '2':
                       clrscr();
                       printf("Process Payroll \n\n\n");
                       printf("Enter Employee Number: ");
                       scanf("%i", &y);
                       for(x=1;x<=z;x++)
                        {
                          if(y == emp[x].id)
                           {
                            printf("Employee Name: %s, %s %s", emp[x].sname, emp[x].fname, emp[x].mi);
                           }
                          
                        }
                      
                      break;
                   }
              }
    while(choice!= '5');
    }
    The first case would enter the data of an employee... And the second case would search the ID number and will display the complete name of the employee. I've tried the method for loop and if, but it will terminate the case immediately without showing the output. What could be wrong with the method I used? Thank you in Advance...

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by jappy512 View Post
    Hi guys... Good Day!

    I'm having a problem with my code below.
    I'm not suprised. There are several things wrong with your code, mainly non-standard functions being used aplenty, seemingly without any prototyping, declaring your struct definition inside main a little weird and not very useful and you have an extra } at the end of the second case statement, at first glance.

    EDIT: forgot to mention the fact that you have no return call in main. main() should return an INT to let the calling OS know how things went.

    Quote Originally Posted by jappy512 View Post
    The first case would enter the data of an employee... And the second case would search the ID number and will display the complete name of the employee. I've tried the method for loop and if, but it will terminate the case immediately without showing the output. What could be wrong with the method I used? Thank you in Advance...
    What is the question exactly? I'm a little confused.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This:
    Code:
    scanf("%s" , &sel);
    Can never be. A char can never be a string - it has no space for itself, and the end of string char. Change that.

    Char's also need to have the keyboard buffer cleared before you scanf() for them. If you don't, in your program, you will get a newline, instead of what you want. It will look like the scanf() is just being "skipped over". A getchar() will remove one char from the keyboard buffer, and then the getchar() will appear to be "skipped over", and the program will wait for the input on the next scanf() of the char.

    In your search, you're looking to match an int. But your struct has id as a float.
    You need them both to be the same data type. Why would an ID by a float? "I'm employee ID number 124.783"? I don't see that working.

    Having variables with names like 'z' is insufferable, really. z is an index, so 'i' might be good, or index. It's also a counter of employees, so emp_num might work well. 'z' is zee ........z, however.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    I corrected your code. You made some mistakes as Adak said. Another one thing, You are avoiding the 0th index in an array. Is there any specific reason for this?. This is not a problem in your code. But you allocated the static memory to the array.After allocated the memory other's can't use the memory space. But why you didn't use the space?.This may be a problem while you writing a big code.(project).Please avoid to miss the index in an array.This is my opinion.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
            struct Payroll_System
            {
                    char fname[40], sname[40], mi[2];
                    int id, bpay;
            }
            emp[5];
            int z=0 , x, y;
    
            char sel;
            char choice;
            do
            {
                    printf("PAYROLL SYSTEM\n\n");
                    printf("MENU:\n 1. Enter Employees Record\n 2. Process Payroll\n 3. Payroll Summary\n 4. Employee Payslip\n 5. Exit\n");
                    printf("Enter your choice: ");
                    choice=getc(stdin);
                    switch(choice)
                    {
                            case '1':
                                    do
                                    {
                                            printf("%d\n",z);
                                            printf("Write Record \n\n\n");
                                            printf("Employee Number: ");
                                            scanf("%i", &emp[z].id);
                                            printf("Employee Name\n");
                                            printf("First Name: ");
                                            scanf("%s", &emp[z].fname);
                                            printf("Middle Initial: ");
                                            scanf("%s", &emp[z].mi);
                                            printf("Last Name: ");
                                            scanf("%s", &emp[z].sname);
                                            printf("Basic Pay (Daily): ");
                                            scanf("%i", &emp[z].bpay);
                                            printf("Employee: %s, %s %s \nSuccessfully Added!\n\n\n", emp[z].sname, emp[z].fname, emp[z].mi);
                                            printf("Add New Record (y/n): ");
                                            getchar();
                                            scanf("%c",&sel);
                                            z++;
                                    }
                                    while(sel != 'n');
                                    break;
                            case '2':
                                    printf("Process Payroll \n\n\n");
                                    printf("Enter Employee Number: ");
                                    scanf("%d", &y);
                                    for(x=0;x<z;x++)
                                    {
                                            if(y == emp[x].id)
                                            {
                                                    printf("Employee Name: %s, %s %s", emp[x].sname, emp[x].fname, emp[x].mi);
                                            }
    
                                    }
    
                                    break;
                    }
            }
            while(choice!= '5');
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. C++ thread inside a class hang issue
    By diefast9 in forum C++ Programming
    Replies: 11
    Last Post: 10-21-2009, 07:18 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Replies: 4
    Last Post: 03-09-2002, 01:22 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM