Thread: struct in C problem

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    22

    struct in C problem

    Here is the question:
    Suppose we have the user-defined structure baby, including:
    · Name is a string
    · BirthYear is an integer
    · BirthMonth is an integer
    · Weight is a real number
    · Sex is an integer that receives only 2 values :1 for boys and 0 for girls
    a) Read information about babies into array B until we get weight 0 .
    b) Read month M (1 ≤ M ≤ 12) and year Y (2000 ≤ Y ≤ 2009)from the keyboard. Display information of babies that were born in month M of year Y with the following format:
    Name Weight Sex
    Suri Cruise 3.45 F
    Romeo Beckham 4.35 M

    · Name is displayed in a 40 character space, left justified
    · Weight is displayed using ten characters with two digits after the decimal point, right justified.
    · Sex is displayed with ‘M’ and ‘F’
    Write message “No baby was born in the month M/Y” if no baby matches.

    And here is my code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    struct baby
        {
            char Name[50];
            int BirthYear;
            int BirthMonth;
            float Weight;
            int Sex; //1 for boys and 0 for girls
        };
    
    
    void main()
    {
        int i=0, M, Y, num, flag=0;
        struct baby B[50];
        clrscr();
    
        do
        {
            i++;
            printf("Enter Name: ");
            fflush(stdin);
            gets(B[i].Name);
            printf("Enter BirthYear: ");
            fflush(stdin);
            scanf("%d", &B[i].BirthYear);
            printf("Enter BirthMonth: ");
            fflush(stdin);
            scanf("%d", &B[i].BirthMonth);
            printf("Enter Weight: ");
            fflush(stdin);
            scanf("%f", &B[i].Weight);
            printf("Enter Sex(1 for boys and 0 for girls): ");
            fflush(stdin);
            scanf("%d", &B[i].Sex);
        }
        while(B[i].Weight != 0);
        num = i; 
    
        do
        {
            printf("Enter month M (1<=M<=12) and year Y (2000<=Y<=2009) separated by space: ");
            scanf("%d %d", &M, &Y);
        }
        while(M>12 || M<1 || Y>2009 || Y<2000);
    
        printf("%-40s%10s\t%s\n", "Name", "Weight", "Sex");
        for(i=1; i<num; i++)
        {
            if(B[i].BirthMonth==M && B[i].BirthYear==Y)
            {
                printf("%-40s%10.2f", B[i].Name, B[i].Weight);
                if(B[i].Sex==1)
                    printf("\tM\n");
                else
                    printf("\tF\n");
                flag = 1;
            }
        }
        if(flag==0)
            printf("No baby was born in the month %d/%d", M, Y);
        getch();
    
    }


    I compiled and ran it using TurboC++. But when i entered just some numbers, the MS-DOS window suddenly closed.
    Can you guys tell me where the problem is?
    Thank you!
    Last edited by Nhân Trần; 11-16-2015 at 10:31 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, your code seems to have no relation to the problem described.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    So sorry. I did copy the wrong question . I edited the new one above

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    So how should I read string from keyboard without using gets? And if fflush(stdin) in this case is wrong, what is your suggestion?
    Once again, thank you

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you completely read the provided links?

    Both of your questions should be answered by throughly reading those links, along with the links provided in those links.

    For example for the gets():
    A resolution
    To get around this problem, ensure you use a more secure function for performing reads. For example,
    fgets() is a buffer safe function.
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Problem
    By cougarsmustangs in forum C Programming
    Replies: 7
    Last Post: 03-24-2013, 12:28 AM
  2. struct struct struct problem....please....help!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  3. Help with a Struct problem :)
    By yrostran in forum C Programming
    Replies: 11
    Last Post: 05-09-2006, 02:14 PM
  4. Struct problem
    By totalfreeloader in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 09:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM