read data

This is a discussion on read data within the C Programming forums, part of the General Programming Boards category; I'm trying to write this program that will read data for one student and compute and print the students average. ...

  1. #1
    in_need
    Guest

    read data

    I'm trying to write this program that will read data
    for one student and compute and print the students
    average.
    1)Define the template of a data structure to contain
    the student's complete name,course number
    (itec21011101), social security number(must be a long) and four test
    grades(values 0 to 110), and an integer average. Define the template as
    a global.

    2) In main define the structure variable. The program
    should pass a function to a pointer in the the
    structure variable in main. The function will read in student's
    name,course number(itec21011101), social security number and accept the
    name for a max length of 20 characters, course number 11 characters and
    social security number for 9 digits. (Use the function to get an integer
    from lab 2 with the modification to allow for only 9 digits.)

    There should be a separate function(Use the function
    to get an integer from lab 2 ) to read in one grade
    and and return it to main. This las function should
    be executed four times. Check each grade and make
    sure it is no more than three digits and the value of
    each grade should be 0 to 110. Each grade should be in
    the structure.

    I must use the get avgerage function to compute the
    grades dropping the lowest grade. When the average is
    returned put in the structure average in main. Print
    all information to the printer with all identifying
    words from main.

    I've been trying to fix this code to use the get_integer and get_avgerage function to include in the program. I haven't had much success with it and so I
    left them as separate files and did not code them in my main program Lab3.c. What needs to be done so that I can use these two functions?
    Below is the code for this lab. What's wrong with this code?

    *-------------------------------------------------*/
    /* Program Name: lab3.c */
    /* program to enter information for up to 4 */
    /* grades. The program prints a report */
    /* based on the numbers entered. */
    /*-------------------------------------------------*/

    /*---------------------*/
    /* included files */
    /*---------------------*/
    #include <stdio.h>
    #include <stdlib.h>
    #include "get_int.h"
    #include "get_avg.h"

    /*---------------------*/
    /* defined constants */
    /*---------------------*/
    #define MAX 4
    #define YES 1
    #define NO 0

    /*---------------------*/
    /* variables */
    /*---------------------*/

    struct record {
    char course_num[11+1]; /* course number + NULL */
    char name[20+1]; /* last name + NULL */
    long ssn[9+1]; /* social security + NULL */
    int grade; /* student grades */
    int average; /* student average */

    };

    struct record list[MAX]; /* declare actual structure */

    int last_entry = 0; /* total number of entries */

    /*---------------------*/
    /* function prototypes */
    /*---------------------*/
    void main(void);
    void get_data(void);
    void display_report(void);
    int continue_function(void);
    void clear_kb(void);

    /*---------------------*/
    /* start of program */
    /*---------------------*/

    void main()
    {
    int cont = YES;
    int ch;

    while( cont == YES )
    {
    printf( "\n");
    printf( "\n MENU");
    printf( "\n ========\n");
    printf( "\n1. Enter data");
    printf( "\n2. Print report");
    printf( "\n3. Quit");
    printf( "\n\nEnter Selection ==> ");

    ch = getchar();

    fflush(stdin); /* remove extra characters from keyboard buffer */

    switch( ch )
    {
    case '1': get_data();
    break;
    case '2': display_report();
    break;
    case '3': printf("\n\nThank you for using this program!\n");
    cont = NO;
    break;
    default: printf("\n\nInvalid choice, Please select 1 to 3!");
    break;
    }
    }
    }

    /*-----------------------------------------------------------*
    * Function: get_data() *
    * Purpose: This function gets the data from the user. It *
    * continues to get data until either 4 grades are *
    * entered, or the user chooses not to continue. *
    * Returns: nothing *
    * *
    *-----------------------------------------------------------*/

    void get_data(void)
    {
    int cont;

    for ( cont = YES; last_entry < MAX && cont == YES;last_entry++ )
    {
    printf("\n\nEnter information for Person %d.",last_entry+1 );
    printf("\n\nEnter name: ");
    gets(list[last_entry].name);
    printf("\nEnter ssn in 123456789 format: ");
    scanf("%ld", &list[last_entry].ssn);

    do
    { printf("\n\tGrades (0 - 110): ");
    scanf("%d", &list[last_entry].grade);
    }while ((list[last_entry].grade < 0) || (list[last_entry].grade > 110));

    cont = continue_function();
    }

    if( last_entry == MAX)
    printf("\n\nMaximum Number of grades has been entered!\n");
    }

    /*-----------------------------------------------------------*
    * Function: display_report() *
    * Purpose: This function displays a report to the screen *
    * Returns: nothing *
    * Notes: More information could be displayed. *
    * Change stdout to stdprn to Print report *
    *-----------------------------------------------------------*/

    void display_report()
    {
    long grade_total = 0,
    grand_total = 0; /* For totals */
    int x, y;

    fprintf(stdout, "\n\n"); /* skip a few lines */
    fprintf(stdout, "\n REPORT");
    fprintf(stdout, "\n ========");

    for( x = 0; x <= 110; x++ ) /* for each grade, including 0 */
    { grade_total = 0;
    for( y = 0; y < last_entry; y++ )
    { if( list[y].grade == x )
    { fprintf(stdout,"\n\t%s %s %s %ld",list[y].name,
    list[y].course_num,list[y].ssn);
    grade_total += list[y].grade;
    }
    }
    fprintf(stdout, "\nTotal for grades %d is %d",x,grade_total);
    grand_total += grade_total;
    }
    fprintf(stdout, "\n\n Grade Report totals:");
    fprintf(stdout, "\nAverage Grade is %d", grand_total/last_entry );
    fprintf(stdout, "\n\n* * * End of Report * * *");
    }

    /*-------------------------------------------------------------------*
    * Function: continue_function() *
    * Purpose: This function asks the user if they wish to continue. *
    * Returns: YES - if user wishes to continue *
    * NO - if user wishes to quit *
    *-------------------------------------------------------------------*/

    int continue_function( void )
    {
    int ch;

    printf("\n\nDo you wish to continue? (Y)es/(N)o: ");
    fflush(stdin);
    ch = getchar();

    while( ch != 'n' && ch != 'N' && ch != 'y' && ch != 'Y' )
    { printf("\n%c is invalid!", ch);
    printf("\n\nPlease enter \'N\' to Quit or \'Y\' to Continue: ");

    fflush(stdin); /* clear keyboard buffer (stdin) */
    ch = getchar();
    }

    clear_kb(); /* this function is similar to fflush(stdin) */

    if(ch == 'n' || ch == 'N')
    return(NO);
    else
    return(YES);
    }

    /*--------------------------------------------------------------------*
    * Function: clear_kb() *
    * Purpose: This function clears keyboard of extra characters. *
    * Returns: Nothing *
    * Note: This function could be replaced by fflush(stdin); *
    *--------------------------------------------------------------------*/
    void clear_kb(void)
    {
    char junk[80];
    gets(junk);
    }

    ************************************************** *
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <ctype.h>

    /*-- This function gets the input string and checks for correct values --*/

    void get_integer(char strings[] , int number)
    {
    char dummy [21] ; /*-- string to be used to get correct value --*/
    int flags;
    int index;

    do
    { fflush(stdin) ;
    flags = 0 ;
    gets(dummy) ; /*-- string is entered --*/
    if((strlen(dummy) > number)) /*-- if the string length of dummy is greater than 4 --*/
    { clrscr();
    printf("Enter a digit: ");
    flags = 1;
    continue;
    }
    /*-- this checks for non-digits if the string length is the correct length --*/
    for(index = 0 ; dummy [index] != '\0'; index++)
    if (( isdigit(dummy[index]) == 0 ))
    { clrscr();
    printf("Only digits please.\n");
    flags = 1;
    break;
    }
    }while(flags == 1) ; /*-- continues while input is bad --*/

    /*-- copy the dummy string that is now valid into the user string --*/
    strcpy(strings,dummy) ;
    }



    ************************************************** **




    /*-- this function recieves the input array and size of the array --*/

    int get_average(int numbers[] , int number)
    {
    long lowest;
    float sum = 0;
    int average;
    int index;
    for( index = 0 ; index < number ; index++)
    { if(index == 0) lowest = numbers[0];

    if(numbers [index] < lowest)
    lowest = numbers [index] ;

    sum += numbers [index] ; }
    average = (sum - lowest) / (number - 1+ .5) ; return(int) average ;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Ok, firstly your structure is off
    > social security number(must be a long)
    > long ssn[9+1]; /* social security + NULL */
    It only needs to be
    long ssn;

    > and four test grades(values 0 to 110)
    > int grade; /* student grades */
    Should be
    int grages[4];

    > 2) In main define the structure variable
    struct record list[MAX]; /* declare actual structure */
    But you declared it as a global, so this needs to be moved into the body of main

    > void main(void);
    You should not prototype main, and main returns an int.

    > The program should pass a function to a pointer in the the
    > structure variable in main.
    This basically means that your functions should be declared as

    /* read a student into the current record */
    void get_data( struct student *rec );

    And within main, you would call this with
    get_data ( &list[num_student] );

    > * Note: This function could be replaced by fflush(stdin); *
    Nope - whatever you might think, whatever your tutor or book tells you and no matter if your compiler seems to work, flushing standard input remains an undefined operation.
    It might work for you, but it will not work for me.

    A better way to flush input is
    Code:
    while ( getchar() != '\n' ) continue;
    Later on, you will want to replace your use of gets() with the much better function fgets()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    in_need
    Guest

    Question read_data

    2) In main define the structure variable. The program
    should pass a function to a pointer in the the
    structure variable in main. The function will read in student's
    name,course number(itec21011101), social security number and accept the
    name for a max length of 20 characters, course number 11 characters and
    social security number for 9 digits. (Use the function to get an integer
    from lab 2 with the modification to allow for only 9 digits.)

    There should be a separate function(Use the function
    to get an integer from lab 2 ) to read in one grade
    and and return it to main. This las function should
    be executed four times. Check each grade and make
    sure it is no more than three digits and the value of
    each grade should be 0 to 110. Each grade should be in
    the structure.

    How would I change the get_integer function so that it reads in social security number to allow for only 9?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read() is not reading the whole data
    By maven in forum C Programming
    Replies: 6
    Last Post: 02-18-2006, 06:15 AM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM

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