C Program: Array Structures

This is a discussion on C Program: Array Structures within the C Programming forums, part of the General Programming Boards category; I'm pretty new to C but so far I have been pretty OK when it comes to implementing programs. I ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    6

    C Program: Array Structures

    I'm pretty new to C but so far I have been pretty OK when it comes to implementing programs. I have been working with this program for about a week now, and I'm just about finished with it. The program is suppose to allow the user to input a persons name and then his/her three test scores. This process is looped-which is shown in the sample output- until the user enters "CTRL-Z". I'm certain I have the gist of the program, it's just I'm running out of time and can't get it to work like the sample output.

    The loops in this program have me baffled, and I just need someone to help point out my mistakes, so I can get it running smoothly.

    -----------------------------------------------------
    Code:
    #include<stdio.h>
    
    
    typedef struct {
    char name[60];
    float exam[3]; /* 3 exams*/
    float avg;
    } person_t;
    
    
    int main()
    {
    person_t persons[50]; //declare structure variable array- up to 50 people
    int i, count = 0; //intitializing counting variable
    float avg, sum = 0, sum_v = 0;
    
    
    while(1) // loop repeats until ctrl-Z is pressed
    {
    printf("\nEnter student name <Ctrl-Z to stop>: ");
    scanf("%s",& persons[count].name[i]);
    
    
    for (i = 0; i < 3; i++)
    {
    printf("Enter student's three exams: ");
    scanf("%d", & persons[count].exam[i]); // Allows user to input the values for three values
    sum = sum + persons[count].exam[i]; //summing up the test values
    }
    persons[count].avg = sum/3; //average of the values entered
    printf("\n\nName\t\t\t\t\tExam 1\tExam 2\tExam 3\tAverage\n"); // Should be output in this format
    
    printf("%s\t\t\t\t%f\t %f \t %f \t %.1f\n",persons[count].name, persons[count].exam[0],persons[count].exam[1], persons[count].exam[2], persons[count].avg);
    
    
    sum_v = sum_v +(persons[count].avg);
    count++;
    }
    avg = sum_v/count;
    printf("\n\nThe class average is %.1f",avg);
    
    
    system("pause");
    return 0;
    }
    

    Expected Output:
    --------------------------------------------------------------------------

    Name:  Question.jpg
Views: 627
Size:  65.5 KB

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,652
    Code:
    scanf("%s",& persons[count].name[i]);
    This line is not correct. The array name without the brackets will act as a pointer to that array (hence you wouldn't need the address-of operator either). Also, "scanf()" will stop reading at any whitespace (including a space), so you couldn't enter a first and last name (as indicated in your expected output). "fgets()" would be better for this purpose.

    Also, you should have two separate loops - the first one to receive user input, then when that's done, a second one to print the output.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    'i' is undefined
    Code:
    scanf("%s",& persons[count].name[i]);

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Is this better?

    Code:
     
    typedef struct {
    Code:
    char fName[60];
    char lName[60];
    float test[3]; /* 3 tests */
    float avg;
    } student_t ;
    ...
    scanf("%s%s",&persons[count].fName[i],&persons[count].lName[i]);
    I haven't used fgets() yet, would you mind giving me an example of it in use.?

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Code:
    ...
    int i = 0,count = 0;          //intitializing counting variable 
    ...

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,652
    Reference: fgets()
    Example of usage: FAQ > Get a line of text from the user/keyboard (C)

    Also, re-read the second sentence of my previous post.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Yeah, I remember that command now. I don't feel to comfortable implementing it into my program. As for the loop, so should it be two for loops.?

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    Code:
     FILE* dataFile;
    
    
       char name [60];
    
    
       dataFile= fopen ("myfile.txt" , "r");
       if (dataFile== NULL) perror ("Error opening file");
       else {
         if ( fgets (name , 60 , dataFile) != NULL )
           puts (name);
         fclose (dataFile);
       }
       return 0;
    }
    Is this how it would look?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,652
    It depends on what you're trying to accomplish. "while" loops are good if you're waiting for a certain criteria to be met before breaking. "for" loops are good for continuous iteration until a predetermined condition is met. These descriptions are watered down, but should point you in the right direction.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,652
    That's exactly how it looks in the sample code on the page I linked above. Unfortunately, it's going to take more than a simple copy + paste to be useful in your application.

    Read about the arguments of "fgets()" (same link) - it tells you how to redirect the stream to the standard input (keyboard) instead of a file.

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    That was the purpose of my while loop initially.

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Yeah, I remember that command now. I don't feel to comfortable implementing it into my program.
    IMO, it's worth the extra effort in understanding fgets(). Just keep in mind, you might have to handle the '\n'.

    Maybe the tutorial on C-Strings is easier for you to understand?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++: Help with program using array structures
    By We'reGeeks in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2010, 01:47 AM
  2. array of pointers to an array of structures
    By phoneix_hallows in forum C Programming
    Replies: 3
    Last Post: 08-27-2009, 12:13 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  4. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 05:51 AM
  5. Array of Structures
    By TeenyTig in forum C Programming
    Replies: 1
    Last Post: 02-17-2002, 09:34 PM

Tags for this Thread


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