Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C] Using structures for the first time

*
Loyal Member
Joined
Jul 23, 2008
Messages
691
Reaction score
90
Hey, so I'm doing a program in C where basically I have to compute grades of students. The user will send to me the name, their answerkey(since the test is a 20 question true/false format), and then repeat this for 3 more students. Once they have done so, I'm to print their results and the student who got the highest grade amongst the 4.

Here's my code so far:
Code:
#include <stdio.h>

struct stud{
int students[5];
char name[20];
int answerkey[20];
char grade[5];
int correct;
};



void Init();
void readData();
int findGrade();
void printAll();
char grade;
void main() {

int grade;

readData(&s1, &s2, &s3, &s4);
}

void Init () {
int i = 0;
int j = 0;



}


void readData(struct stud *students) {
int correctkey[20];

int i = 0;


        for (i = 0; i < 5; ++i) {

        printf("Please enter the students name \n");
        scanf("%s", students[i].name);
        printf("Please enter their respective answers \n");
        scanf("&d", students[i].answerkey);
        i++;
        }



int findGrade(int *answerkey, int *correctkey, struct stud *students) {
int correct = 0;
int j = 0;
int scorekey, scorehigh;
        for (j = 0; j < 20; j++) {
            if (answerkey[j] == correctkey[j])
        correct++;
        }
        if (correct <= 20) {
        grade = "A";
        }
        else if (correct <= 16) {
        grade = "B";
        }
        else if (correct <= 14){
        grade = "C";
        }
        else if (correct <= 12){
        grade = "D";
        }
        else if (correct <= 11){
        grade = "F";
        }
    /*    if (students.correct[0] > students.correct[1] && students.correct[2] && students.correct[3]) {
        highest = students[0].correct;
        scorekey = students[0].answerkey
        else if (students.correct[1] > students.correct[0] && students.correct[2] && students.correct[3])
        highest = students[1].correct;
        scorekey = students[1].answerkey

        else if (students.correct[2] > students.correct[0] && students.correct[1] && students.correct[3])
        highest = students[2].correct;
        scorekey = students[2].answerkey

        else if (students.correct[3] > students.correct[2] && students.correct[1] && students.correct[0])
        highest = students[3].correct;
        scorekey = students[3].answerkey
*/
}


void printAll () {

        printf("Name\tStudent Answers\t\t\tAnswers-Correct\tGrade");
        printf("%s\t%d\t%d\t%s", student.name, student.answerkey, student.correct, student.grade);
        printf("%s\t%d\t%d\t%s", student.name, student.answerkey, student.correct, student.grade);
        printf("%s\t%d\t%d\t%s", student.name, student.answerkey, student.correct, student.grade);
        printf("%s\t%d\t%d\t%s", student.name, student.answerkey, student.correct, student.grade);

        printf("answerkey \t\t Highest Score");
        printf("%d \t %d", scorekey, highest);

}

The code is a bit incomplete, its just that I feel i'm so fundamentally wrong that I'd like someone who's experienced to fill me in here.

Thanks alot.
 
That one pokemon thing
Loyal Member
Joined
Apr 3, 2010
Messages
1,766
Reaction score
621
I'm not a very experience C user, and there might be more, but:

Code:
if (correct <= 20) {
        grade = "A";
        }
        else if (correct <= 16) {
        grade = "B";
        }
        else if (correct <= 14){
        grade = "C";
        }
        else if (correct <= 12){
        grade = "D";
        }
        else if (correct <= 11){
        grade = "F";
        }

This code will never reach B to F, because the answer is always smaller or equal to 20 (<=). You should use bigger or equal (>=).
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
While what Luxray said is absolutely true and should be the source of the problem there is several stuff in your little program that provokes undefined behavior and ignores several fundamentals of programming...

1. "readData()" takes a pointer to the "stud" struct as parameter while you call it with undefined object references. I guess those are supposed to be student structs. This wont even compile, lol.

2. "Init()" is never referenced to and completely useless the way you wrote it. Do you know what variable scopes are?

3. Never used variable "grade" in main()

4. Access violation in "readData()" as you pass a single pointer not an array

5. "findGrade()" never referenced

6. Several accessing of "stud::students" as if it would be another struct or smth. wont even compile

7. "scanf("&d"...." that wont work...

8. Assigning of ROM memory to RAM memory in "findGrade()"

9. A student struct / class should be unique by it's name. It should rather define a student than contain many of em

Please provide us with a full example or at least bother to copy all important stuff off...
 
Modeler / C++ Coder
Developer
Joined
Feb 6, 2008
Messages
561
Reaction score
483
I will reply shortly with a desent answer.. ^^

Edit:

Code:
#include "stdafx.h"


int g_iStudentCount;
int g_iCorrectAnswers[20];
int g_iHighest;


struct Student
{
    char name[20];
    int answers[20];
    char grade;
    int correct;
};


Student* students;


bool InitGrades()
{
    if(g_iStudentCount && students)
    {
        for(int i = 0; i < g_iStudentCount; i++)
        {
            students[i].correct = 0;


            for(int j = 0; j < 20; j++)
            {
                if(students[i].answers[j] == g_iCorrectAnswers[j])
                {
                    students[i].correct++;
                }
            }


            if (students[i].correct <= 11) { students[i].grade = 'F'; }
            else if (students[i].correct <= 12) { students[i].grade = 'D'; }
            else if (students[i].correct <= 14) { students[i].grade = 'C'; }
            else if (students[i].correct <= 16) { students[i].grade = 'B'; }
            else if (students[i].correct <= 20) { students[i].grade = 'A'; }
            else
            {
                return false;
            }


            if(students[i].correct > g_iHighest)
            {
                students[i].correct = g_iHighest;
            }
        }


        return true;
    }




    return false;
}


void PrintGrades () 
{
    for(int i = 0; i < g_iStudentCount; i++)
    {
        printf("Name: [%s] Answers:", students[i].name);
        for(int j = 0; j < 20; j++)
        {
            printf("[%d] ", students[i].answers[j]);
        }    
        printf("Correct:[%d] Grade:[%s]\n\n", students[i].correct, students[i].grade);
    }


    printf("Correct Answers: ");
    for(int i = 0; i < 20; i++)
    {
        printf("[%d] ", g_iCorrectAnswers[i]);
    }
    printf("Highest Grade:[%d]\n", g_iHighest);
}


int _tmain(int argc, _TCHAR* argv[])
{
    students = 0;
    g_iStudentCount = 0;
    g_iHighest = 0;


    printf("Please enter the correct 20 answers, enter in between.\n");
    for(int i = 0; i < 20; i++)
    {
        scanf("%d", &g_iCorrectAnswers[i]);
    }


    printf("Please enter student count.\n");
    scanf("%d", &g_iStudentCount);


    if(g_iStudentCount)
    {
        students = new Student[g_iStudentCount];
        if(students)
        {
            for(int i = 0; i < g_iStudentCount; i++)
            {
                printf("Student[%d] Please enter the Students name.\n", i);
                scanf("%s", students[i].name);


                printf("Student[%d] Please enter the Students answers, enter in between.\n", i);
                for(int j = 0; j < 20; j++)
                {
                    scanf("%d", &students[i].answers[j]);
                }
            }


            if(InitGrades())
            {
                PrintGrades();


                printf("Press any key to exit.");
                scanf("");
            }
            else
            {
                printf("Error: Could not initialize the Grades.\n");
            }


            delete [] students;
            students = 0;
        }
    }


    return 0;
}

Hope it helps you any further :) i just made it as a c++ console app and tested it myself mostly.. seems to work ;)
 
Last edited:
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
@Matynator please don't encourage people to dump their homework assignments here by providing complete answers. No offense on the OP - you obviously tried to do the work yourself first and didn't ask for a complete solution either.
 
Modeler / C++ Coder
Developer
Joined
Feb 6, 2008
Messages
561
Reaction score
483
@Matynator please don't encourage people to dump their homework assignments here by providing complete answers. No offense on the OP - you obviously tried to do the work yourself first and didn't ask for a complete solution either.

Ok so actually taking the time to supply a descent answer for this guys homework to be nice is not allowed these days anymore?..
If he wants to learn he will have to read it and fix what is still missing for his assignment.. i just gave him a startpoint :p
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
Oh my bad then. Nothing in your post said it's a skeleton so I assumed. Obviously I'm not saying it's a bad thing to be helpful either even if you did give out full answers; I just don't like the lazy bastards at the other end one bit. :)
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
Matynator please don't encourage people to dump their homework assignments here by providing complete answers. No offense on the OP - you obviously tried to do the work yourself first and didn't ask for a complete solution either.

I agree, hence why I posted tips for him to think about with which he could have been able to find out what his problems are by himself maybe...
I must agree that this looks like a homework which was unsolved. The amount of code posted is very high. It's not a specific problem he is stuck with it's just very broad.
But well he has a perfect example by Matynator now... If he just want's to copy paste :p
 
Modeler / C++ Coder
Developer
Joined
Feb 6, 2008
Messages
561
Reaction score
483
Oh my bad then. Nothing in your post said it's a skeleton so I assumed. Obviously I'm not saying it's a bad thing to be helpful either even if you did give out full answers; I just don't like the lazy bastards at the other end one bit. :)

I agree, but think of this.. if he would copy paste this code for his assignment.. sure he would maybe pass this homework assignment.. but what happens with the next one?

People that copy and paste wont learn.. and will fail automatically if they don't spend the energy to learn what they are supposed to..
But on the other hand i don't mind helping out here and there.. ;)

Everyone has to start somewhere.. and rather not let them do it the way i did it, no teacher no help.. no one to really step to for issues or problems.. cant solve an issue?.. google it!.. even though it helped me in the end...
 
Back
Top