Thread: Help with a very simple C program....Already written, not working right!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    Post Help with a very simple C program....Already written, not working right!

    Hey,

    I have to write a program for class that takes in a last name, first name, and exam score and stores them into seperate arrays. No structs in this however. Then I have to print the high score, low score, and avg score. Thats it. I thought that I had it right but it doesnt work correctly when I run it, even though it compiles fine.

    Heres my program...

    Code:
    #include <stdio.h>
    
    #define CLASS_SIZE 10
    #define NAME_SIZE 20
    
    int getInfo(char[][20], double[], char[][20], int);
    void printResults(char[][20], double[], char[][20], int);
    void doCalcs(double[], int);
    
    int main()
    {
            int count = 0;
            char first[10][20];
            char last[10][20];
            double score[10];
    
            count = getInfo(first, score, last, count);
            doCalcs(score, count);
            printResults(first, score, last, count);
    
            return 0;
    }
    
    
    int getInfo(char first[][20], double score[], char last[][20], int count)
    {
            char again='y';
    
            while(again=='y')
    {
            printf("Please enter the student's first name -- ");
            scanf("%s", first[count]);
            printf("Please enter the student's last name -- ");
            scanf("%s", last[count]);
            printf("Please enter the exam score --  ");
            scanf("%lf", &score[count]);
    
            count++;
    
            printf("Would you like to enter another record? (y/n)");
            //scanf("%c", &again);
            scanf("%s", &again);
    }
            return count;
    }
    
    void doCalcs(double score[], int count)
    {
            int i;
            double high;
            double low = 105;
            double avg;
            double total;
    
            for(i=0; i<count; i++)
            {
            if(score[i] > high)
                    {
                    high = score[i];
     		}
            if(score[i] < low)
                    {
                    low = score[i];
                    }
            }
    
            for(i=0; i<count; i++)
            {
            total = total + score[i];
            }
    
            avg = total/count;
    
            printf("Total number of students = %d\n", count);
            printf("Highest score = %lf\n", high);
            printf("Lowest score = %lf\n", low);
            printf("Average score = %lf\n", avg);
    }
    
    void printResults(char first[][20], double score[], char last[][20], int count)
    {
            int i;
            for(i=0; i<count; i++)
            {
            printf("%s, %s %4.2lf\n", last[i], first[i], score[i]);
            }
    }
    This is what Im getting when I enter some information...

    Please enter the student's first name -- Joe
    Please enter the student's last name -- Smith
    Please enter the exam score -- 30
    Would you like to enter another record? (y/n)y
    Please enter the student's first name -- Bob
    Please enter the student's last name -- White
    Please enter the exam score -- 80
    Would you like to enter another record? (y/n)n
    Total number of students = 2
    Highest score = 0.000000
    Lowest score = 0.000000
    Average score = 37270968922511555218815399770931566435968430750338 28121034433494206368125488490594734490445484833985 42932704708245693379119842581045603106586418751351 90047137854599889866604204928347406994246874588887 4194112758707602325504.000000
    7N, 0.00
    , 0.00
    Segmentation fault


    Any help would be greatly appreciated!!!! Thank you!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Initialize all the variables involved in your calculations, for example

    double high = -105.0;
    double low = 105.0;
    double total = 0.0;
    double avg = 0.0;

    Utherwise you will have an undetermined initial value for these very important numbers.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("&#37;s", &again);
    you cannot do this

    even if the user pressed only 1 char and enter
    scanf will store 2 chars in the pointer provided - the char pressed and nul-char

    if user enters something longer - even more characters will be stored, while you have only place for 1 char
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Thank you so much! It works! I had no idea!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    For some reason when I put &#37;c in there instead, it wont let me enter a character! Its just like the statement is not even there!

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    I have no idea why my program just worked once when I changed the initializations, and now its not working again after no other changes...weird

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Am i allowed to say scanf("%c", &again) in that spot? Because like I have it commented out, if I have it just like that, the program does not pause to allow me to enter a char and just keeps running outside the loop and finishes...

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    ya u can use scanf("&#37;c", &again).. but before that u should clear the buffer.. for that u can just type getchar();

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    That works! Thats crazy, why do you have to clear the buffer, and why does getchar() do it for you! Ha off to google....

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    thats because in ur program, inside while loop ur adding a value to the charecter variable. as again is a charecter variable only a charecter gets in to it. but as you press enter after entering a charecter variable even the \n value also will be in the buffer of stdin. so by using getchar() function we are just emptying the buffer.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Awesome i got ya! Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  2. Builder C++ large files support for simple C program
    By Murfury in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 03:47 PM
  3. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 05:05 AM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM