Thread: Need help with a project

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    21

    Need help with a project

    Hello everyone, this is my first post on the forum.

    I need to do a project for my uni in C, unfortunately I'm learning C for quite short time, which is very stupid of me, now I started spending a lot of time with C, but everything is still very mixed up for me, and time for my project is running out.

    I have to write following program:

    a) Define a structure called "data" which contains 2 elements, string to store name and string to store surename

    b)Define structure called "student" which contains: structure "data", an array to store grades and variable that stores the average value of grades

    c)In main() declare an array of structures type "student"(SIZE=4), initialize the elements of structure "data" with names of the students

    Use functions for d,e,f,g

    d)Ask user to enter grades of students and put them in element "grades" in a structure

    e) Calculate the average value of grades for each structure and pin it on to the proper element

    f) Print informations contained in each structure

    g) Print the average value of grades for each student


    I need to do it in at least 2 files, so I figured I'm gonna put functions in one file and main() in the other.

    You can see my code below, so far I have covered a,b,c,d, however I didn't use functions. At the moment I'm pretty much stuck, so help will be greatly appreciated!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 4
    
    
    struct data {
        char name[20];
        char surename[20];
    };
    struct student {
        struct data data_s;
        float average;
        int grades[10];
    };
    
    
    int main(int argc, char* argv[])
    {
        struct student students[MAX];
        int counter = 0;
        printf("Name of the first student: ");
        gets(students[counter].data_s.name);
        while (counter < MAX) {
            printf("Student's surename: ");
            gets(students[counter++].data_s.surename);
            if (counter < MAX) {
                printf("Name of the next student: ");
                gets(students[counter].data_s.name);
            }
        }
        int i;
        int counter_grades;
        for (i = 0; i < MAX; ++i) {
            printf("Grades of the student %d: ", i + 1);
            for (counter_grades = 0; counter_grades < MAX; ++counter_grades) {
                if (0 == scanf("%f", &students[i].grades[counter_grades]))
                    break;
            }
            fflush(stdin);
        }
        system("PAUSE");
        return 0;
    }
    Last edited by Glorn; 02-03-2015 at 02:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    A development process
    Shows you how to progress from 'all in main' to having functions.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Quote Originally Posted by Salem View Post
    A development process
    Shows you how to progress from 'all in main' to having functions.
    I'm not quite familiar with some of the things included there, maybe it'll help, though I need to hurry because i got only 1 day :S Thanks anyway

  4. #4
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Building upon the advice Salem game. I suggest that you turn on warnings for your compiler, it will catch errors for you. You also have buffer overflow in you code, because you read in unvalidated input with gets(3), use fgets(3) instead.

    Also, you should not use fflush(3) on an input stream like stdin, doing so is undefined behavior.

    You should definitely not depend on buffer overflows and undefined behavior.

  5. #5
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by Glorn View Post
    I'm not quite familiar with some of the things included there, maybe it'll help, though I need to hurry because i got only 1 day :S Thanks anyway
    The howto that Salem linked to you is something you should take to heart, if you follow that, then you will be a decent programmer in no time. Really condensed it more or less says "Break down your problem into smaller steps, solve each step at a time and test each step before you go on to the next.".

    That way you know that the new code you write is built upon working code. We see too many people that come here who have written a massive amount of code before testing once, that way you pile errors upon errors and it gets really hard to debug.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Here's a hint for d)
    Code:
    int scanGrades( struct student *hStudent )
    {
      // Your code
      return did_you_fail_or_succeed;
    }

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    The use of the struct student can be made simpler with a typedef and a simple _ character, I'll leave learning how to you

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > though I need to hurry because i got only 1 day
    You made the bed, you lie in it.

    We're not here to rescue your ass from your poor time management skills.

    Next time, post when you have a week to go, then there will be plenty of time to explain what you need to know, and help you really learn something useful.

    All that could happen now is for some mistaken soul to hand you your answer on a plate, and that just isn't going to happen from any of the regular helpers (on pretty much any forum you'll find).
    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.

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    Quote Originally Posted by Salem View Post
    > though I need to hurry because i got only 1 day
    You made the bed, you lie in it.

    We're not here to rescue your ass from your poor time management skills.

    Next time, post when you have a week to go, then there will be plenty of time to explain what you need to know, and help you really learn something useful.

    All that could happen now is for some mistaken soul to hand you your answer on a plate, and that just isn't going to happen from any of the regular helpers (on pretty much any forum you'll find).
    I do realize that and it's entirely my fault. However, the facts are that I must do it by tomorrow, if not I'll have to cheat it by downloading some code from the internet. Whatever happens,I want to learn C, so your advices are appreciated

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    I've just read e) f) and g), the hint for d) works for those as well

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    What's wrong with this function? It compiles but it doesnt print anything.

    EDIT: Or whatever, I'll just put the whole function, so you can see declaration and everything:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 4
    
    
    struct data {
        char name[20];
        char surename[20];
    };
    struct student {
        struct data data_s;
        float average;
        int grades[10];
    };
    int grades_f(const struct student* grades_in);
    
    
    int main(int argc, char* argv[])
    {
    
    
        struct student students[MAX];
        int grades_f(students);
        int counter = 0;
        printf("Name of the first student: ");
        gets(students[counter].data_s.name);
        while (counter < MAX) {
            printf("Student's surename: ");
            gets(students[counter++].data_s.surename);
            if (counter < MAX) {
                printf("Name of the next student: ");
                gets(students[counter].data_s.name);
            }
        }
    
    
        int grades_f(const struct student* grades_in)
        {
            int i;
            for (i = 0; i < MAX; i++, grades_in++) {
                printf("Grades of student: %d", i + 1);
                scanf("%f", grades_in->grades);
            }
        }
        system("PAUSE");
        return 0;
    }
    Last edited by Glorn; 02-04-2015 at 04:24 PM.

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    You can't declare/implement a function inside another function

  13. #13
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    your compiler should have complained at you for that anyway, make sure whatever setting that forces all warnings to be shown is on, normally it is called Wall

  14. #14
    Registered User
    Join Date
    Feb 2015
    Posts
    21
    That was extremely stupid of me not to notice that, however I put my function outside main() and it's still the same...

  15. #15
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    The only thing I can think of right now is that system("PAUSE");
    Check what your cmdline is supposed to do upon receiving that command.

    Edit By the way what is the actual output of the program, does it print the string literals only or is there simply nothing in the window?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-02-2013, 06:45 PM
  2. Replies: 5
    Last Post: 02-23-2013, 03:37 PM
  3. Classes Project (Not class project)
    By adam.morin in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2011, 01:48 AM
  4. Paid project - The Free Marketing Project
    By sharefree in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-27-2010, 02:15 PM
  5. your 11th & 12th grade c++ project or similar project
    By sleepyheadtony in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-13-2002, 05:14 PM

Tags for this Thread