Simple program structure.

This is a discussion on Simple program structure. within the C Programming forums, part of the General Programming Boards category; I have a problem in the followring code. After collecting the information for 3 employees, the program displays 2. Why? ...

  1. #1
    Unregistered
    Guest

    Question Simple program structure.

    I have a problem in the followring code. After collecting the information for 3 employees, the program displays 2. Why?


    "The program should loop around the user prompts three times to read the information for three entries in the employee_database"


    #include <stdio.h>

    /* first define a single record */

    typedef struct {

    char forename[10];
    char surname[10];
    int age;
    float salary;
    } record_template;


    /* now to create the array if records*/

    int main()
    {

    record_template employee_database[20];
    int t;
    int counter;

    for (t=0; t<3; t++)

    {

    printf("Enter your forename\n");
    scanf("%s",&employee_database[t].forename);

    printf("Enter surname\n");
    scanf("%s", &employee_database[t].surname);

    printf("Enter age: ");
    scanf("%d", &employee_database[t].age);

    printf("Enter salary: ");
    scanf("%f", &employee_database[t].salary);
    }

    for (counter=0; counter <3 ; counter++)
    {
    printf("\n\n");
    printf("Employee no. %d\n", counter);
    printf("Surname: %s\n", employee_database[counter].surname);
    printf("Age : %d years \n", employee_database[counter].age);

    }

    }

    thanks a lot

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It displays three lots of output when I run it. (ranging 0 to 2)

    My comments though:
    >scanf("%s", &employee_database[t].forename);
    doesn't do bounds checking, so what if the user enters more than 9 characters for this? You'll run into trouble, is what!

    >scanf("%d", &employee_database[t].age);
    You should check the user entered a valid number.
    See this post for more info.

    Add a return (0); statement at the end of main();

    And just to be pedantic , you use two different variables as loop counters, you only really need to use one in this code.

    Hope this helps
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 04:19 PM
  3. Simple Structure program
    By dopejack in forum C Programming
    Replies: 19
    Last Post: 12-15-2007, 10:30 AM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  5. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21