Thread: program error

  1. #1
    Unregistered
    Guest

    Question program error

    I'm trying to write this program that will read data for one student and compute and print the students average.
    Lab 2 functions:
    get_integer - this function will be passed a character array(empty) and the maximum number of digits to be entered. The function should accept a number from the keyboard into the character array and check for input of only digits and for the maximum number. You should stay in the function until an acceptable value is entered. This function should work for an int or a long.
    get_average - this function will be passed an array of integer values that represents how many elements are in the array. It will return the average of these values, dropping the lowest value. The average should be rounded off to an integer value before returning it to the calling function.

    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 exactly 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. This is code. It is supposed to do the error checking as I enter the name, ssn, and course number and the average is coming up wrong to. Please tell me what I'm doing wrong in this code!!!

    #include <stdio.h>
    #include <stdlib.h>

    struct student {
    char name[21];
    char cnum[12];
    long ssn;
    int grades[4];
    int average;

    };
    void get_data(struct student *);
    void get_integer(char string[] , int number);
    int get_average(int numbers[] , int number);

    int main(void)
    {

    char string[21];
    int test = 0;
    int index = 0;
    int flags;
    struct student record;
    get_data(&record);
    do{
    flags = 0;
    printf("Enter the grade %d\n", index + 1);
    get_integer(string, 3);
    test = atoi(string);
    if(test<110 &&test>0)
    record.grades[index++]=test;
    else
    flags = 1;
    }while ((flags==1)||(index < 4));
    printf("The student's name is %s:\n", record.name);
    printf("The ssn is %ld:\n", record.ssn);
    printf("The course number is %s:\n", record.cnum);

    printf("The average of the four student grades is %d:\n", record.average);
    return;
    }
    void get_data(struct student *record)
    {
    char string[21];
    char string2[10];
    char string3[12];
    string[0]=21;
    string2[0]=10;
    string3[0]=12;
    printf("Please enter the student's name:\n");
    cgets(string);
    strcpy(record->name, string);


    printf("Please enter social security number:\n");
    get_integer(string2, 9);
    while(strlen(string2)!=9)
    {printf("\nEnter only 9 digits!");
    printf("Please enter the student's ssn:\n");
    get_integer(string2, 9);}
    record->ssn=atol(string2);
    printf("Enter the course number:\n");
    gets(string3);
    while(strlen(string3[1])!=11){

    printf("\nEnter only 11 characters!");
    printf("\nPlease enter the student's course number:\n");
    gets(string3);}
    strcpy(record->cnum, string3);
    }
    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) /*-- to find the lowest set the first value in the array --*/
    /*-- to the lowest variable and if a following value in the --*/
    /*-- comparison is larger set it to the lowest variable --*/
    lowest = numbers[0];

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

    sum += numbers [index] ; /*-- sum up all the numbers --*/
    }
    average = (sum - lowest) / (number - 1)+ .5; /*-- get the average minus the lowest --*/
    return(int) average ; /*-- return the average as a long value --*/
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no way I'm going to wade through all that code.
    However, here are a few points of interest:

    char string[21];
    char string2[10];
    char string3[12];
    string[0]=21;
    string2[0]=10;
    string3[0]=12;
    What exactly is the purpose of those three assignment lines?

    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
    This would be better written:

    Code:
    flags = 1; /* set this first */
    do {
        /* fflush(stdin);  <---- this is not ANSI, avoid its use */
        printf("Enter a whole number: ");
    
        /* read input */
        fgets( stdin, dummy, max_string_length );
    
        if( strlen( dummy ) > number ) continue;
    
        for( index=0; dummy[index] && index < number; index++ )
            if( !isdigit( dummy[index] ) break;
    
        if( index < number ) continue;
    
        flags = 0;
    }while( flags ) ;
    Granted, there are different ways to do it...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    fgets( stdin, dummy, max_string_length );
    Should be:

    fgets( dummy, max_string_length, stdin );
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM