Thread: C Problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    C Problem

    This is my first C programming class (and its online!)... I'd really appreciate any help you can provide... Here's what's in the book-

    Using the student record example in Section 12.2, "Accessing a Member," on page 394, write a function that prints the average for each student in a class. Let an A grade have Value 4, a B grade have value 3, and so forth.

    pg 394-

    cl_info.h

    #Define CLASS_SIZE 100

    struct student {
    char *last_name;
    int student_id;
    char grade;
    };

    **********************

    #include "class_info.h"

    int main(void)
    {
    struct student temp, class[CLASS_SIZE];
    ..........

    then it says :

    assign values to memebers

    temp.grade = 'A'
    temp.last_name = "Bushker";
    temp.student_id = 590017;



    ************************
    * Here's what I have so far... *
    ************************

    Code:
    
    cl_info.h
    
    #include <stdio.h>
    
    #define CLASS_SIZE 100
    //CLASS_SIZE is the total number of grade entries
    
    
    struct student
    {
    	char *last_name;
    	int  student_id;
    	char grade;
    };
    
    ***************************
    stdt_avg.cpp
    
    
    //Programming in C
    
    #include "cl_info.h"
    
    void stdt_avg(struct student class[])
    {
        int i = 0;
    	double assignments = 0.0, stdt_total = 0.0;     // # of assignments and Total of all grades
    	double stdt_avg = 0.0;                   // a Students Average Grade
    
    	curr_student.grade = class[i].grade
    	curr_student.name = class[i].name;
        curr_student.id = class[i].id;
    	
    	for (i = 0; length > i && ; ++i)
    	{
    		if (strcmp(curr_student, class[i].id))
    		{
    			switch(class[i].grade)
    				case(class[i].grade == 'A' ) 
    				stdt_total+=4;
    				case(class[i].grade == 'B' ) 
    				stdt_total+=3;
    				case(class[i].grade == 'C' ) 
    				stdt_total+=2;
    				case(class[i].grade == 'D' ) 
    				stdt_total+=1;
    				case(class[i].grade == 'F' ) 
    				stdt_total+=0;
    
    				assignments++;
    
    				stdt_avg = (stdt_total/assignments);
    				printf("%s (Student ID #: %d) average for this class is %f\n",
    					curr_student.name, curr_student.id, stdt_avg);
    
    		}
    		else
    		{
    			curr_student.name = class[i].name;
    			curr_student.id = class[i].id;
    			stdt_total = grade_value(class[i].grade);
    			assignments = 0;
    		}
    	}
    	printf("\n\n%s",
    		"  Grade Scale: 3-4 = A\n"
    		"               2-3 = B\n"
    		"               1-2 = C\n"
    		"               0-1 = D\n");
    }
    
    int main(void)
    {
    	struct student  class{CLASS_SIZE], *p = &class;
    
    	(*p)[0].grade = 'B';       // an array pre-filled with various students' grades
        (*p)[0].last_name = "Seinfeld";
        (*p)[0].student_id = 001;
    
        (*p)[1].grade = 'A';
        (*p)[1].last_name = "Bond";
        (*p)[1].student_id = 007;
    
        (*p)[2].grade = 'C';
        (*p)[2].last_name = "Newman";
        (*p)[2].student_id = 003;
    
        (*p)[3].grade = 'B';
        (*p)[3].last_name = "Seinfeld";
        (*p)[3].student_id = 001;
    
        (*p)[4].grade = 'B';
        (*p)[4].last_name = "Bond";
        (*p)[4].student_id = 007;
    
        (*p)[5].grade = 'C';
        (*p)[5].last_name = "Newman";
        (*p)[5].student_id = 003;
    
        (*p)[6].grade = 'C';
        (*p)[6].last_name = "Seinfeld";
        (*p)[6].student_id = 001;
    
        (*p)[7].grade = 'C';
        (*p)[7].last_name = "Bond";
        (*p)[7].student_id = 007;
    
        (*p)[8].grade = 'C';
        (*p)[8].last_name = "Newman";
        (*p)[8].student_id = 003;
    
    	stdt_avg(class[]);
        return 0;
    }
    **************
    I'm getting errors when I compile due to
    'class' missing tag name
    and stdt_avg local function definations are illegal

    thanks!! cedarcreek

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you trying to compile C code with a C++ compiler in which class is a keyword?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    I think of keywords as int, long, switch, case, break, etc

    This should be more like an array with the parameter class of the type pointer to struct student.

    sorry, it takes so long to get back with you, but my internet connection is slower than a two legged centipede. not that I have anything against two legged centipedes--for the record.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This might be your first hint that it's not a C file, but a C++ file:
    Code:
    stdt_avg.cpp
    In such a case, class is treated much like int, long, ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't matter what you think keywords are. It matters what your compiler thinks they are. In C++ class is a keyword. That means, any time you use the word class as a stand alone word, it's a keyword if you are compiling as C++. See, there's a reason Dave asked the question. Not for you to be vague and not answer, rather, to get an answer from you.

    So one more time... Are you compiling as C or as C++?


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

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    first time code writer... gave the book answer I was taught regarding keywords... and I didn't know what he was asking (at the time). I'm using VisualStudio.net Academic... and I think everything is complied in c++ even though its written in C. I'll go back and select another word... to see what happens. Also I found an error already in my code... I've changed one line below
    Code:
    for (i = 0; i < CLASS_SIZE; ++i)

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you sure it's not a { where a [ ought to be?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    thanks for all the help! the errors are down to something manageable. but I'm still not sure exactly how structures work and are passed... that's where 95% of my errors are now. Updated code below...

    Thanks Dave... my complier already caught it and I've fixed it (typo)... the errors down to a manageable level... less than 25. errors were over 100 with the 'class' keyword. guess I'll never forget that mistake!

    Code:
    cl_info.h
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define CLASS_SIZE 100
    //CLASS_SIZE is the total number of grade entries
    
    
    struct student
    {
    	char *last_name;
    	int  student_id;
    	char grade;
    };
    
    ****************
    stdt_avg.cpp
    
    /*Programming in C */
    
    #include "cl_info.h"
    
    void stdt_avg(struct student math[])
    {
        int i = 0;
    	double assignments = 0.0, stdt_total = 0.0;     /* # of assignments and Total of all grades */
    	double stdt_avg = 0.0;                          /* a Students Average Grade */
    
    	char curr_student.grade, curr_student.name, curr_student.id;
    
    	curr_student.grade = math[i].grade;
    	curr_student.name = math[i].name;
        curr_student.id = math[i].id;
    	
    	for (i = 0; i < CLASS_SIZE; ++i)
    	{
    		if (strcmp(curr_student.id, math[i].id))
    		{
    			switch(math[i].grade)
    			{
    			case (math[i].grade == 'A' ):
    				stdt_total+=4;
    			case (math[i].grade == 'B' ): 
    				stdt_total+=3;
    			case (math[i].grade == 'C' ): 
    				stdt_total+=2;
    			case (math[i].grade == 'D' ): 
    				stdt_total+=1;
    			case (math[i].grade == 'F' ): 
    				stdt_total+=0;
    //			default:
    //				printf("Error");
    			}
    
    				assignments++;
    
    				stdt_avg = (stdt_total/assignments);
    				printf("%s (Student ID #: %d) average for this math class is %f\n",
    					curr_student.name, curr_student.id, stdt_avg);
    
    		}
    		else
    		{
    			curr_student.name = math[i].name;
    			curr_student.id = math[i].id;
    			stdt_total = math[i].grade;
    			assignments = 0;
    		}
    	}
    	printf("\n\n%s",
    		"  Grade Scale: 3-4 = A\n"
    		"               2-3 = B\n"
    		"               1-2 = C\n"
    		"               0-1 = D\n");
    }
    
    int main(void)
    {
    	struct student  temp, math[CLASS_SIZE];         //, *p = &math;
    
    	math[0].grade = 'B';       // an array pre-filled with various students' grades
        math[0].last_name = "Seinfeld";
        math[0].student_id = 001;
    
        math[1].grade = 'A';
        math[1].last_name = "Bond";
        math[1].student_id = 007;
    
        math[2].grade = 'C';
        math[2].last_name = "Newman";
        math[2].student_id = 003;
    
        math[3].grade = 'B';
        math[3].last_name = "Seinfeld";
        math[3].student_id = 001;
    
        math[4].grade = 'B';
        math[4].last_name = "Bond";
        math[4].student_id = 007;
    
        math[5].grade = 'C';
        math[5].last_name = "Newman";
        math[5].student_id = 003;
    
        math[6].grade = 'C';
        math[6].last_name = "Seinfeld";
        math[6].student_id = 001;
    
        math[7].grade = 'C';
        math[7].last_name = "Bond";
        math[7].student_id = 007;
    
        math[8].grade = 'C';
        math[8].last_name = "Newman";
        math[8].student_id = 003;
    
    	stdt_avg(student, math[]);
        return 0;
    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you program in C - use .c extention for the file

    You initialize only 9 students, why do you process in the loop 100?
    Last edited by vart; 12-05-2006 at 11:42 PM.
    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

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    updated code - still 7 errors... any ideas?

    C2143: syntax error: missing ';' before '.' on lines 11, 12, 13
    C2086: 'student curr_student' : redefination lines 12, 13
    C2664: 'strcmp' : cannot convert parameter 1 from 'int' to 'const *'

    Code:
    cl_info.h
    **********
    // 12.6 just asks for a function and not an entire program.
    // I wrote an entire program to make sure the function worked,
    // but the program itself isn't very robust or logical in a
    // real world situation.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define CLASS_SIZE 10
    //CLASS_SIZE is the total number of grade entries
    
    
    struct student
    {
    	char *last_name;
    	int  student_id;
    	char grade;
    };
    
    ***********
    stdt_avg.c
    ***********
    
    /*Programming in C */
    
    #include "cl_info.h"
    
    void stdt_avg(struct student math[])
    {
        int i = 0;
    	double assignments = 0.0, stdt_total = 0.0;     /* # of assignments and 
    
    Total of all grades */
    	double stdt_avg = 0.0;                          /* a Students Average 
    
    Grade */
    
    	student curr_student.grade = math[0].grade;
    	student curr_student.last_name = math[0].last_name;
    	student curr_student.student_id = math[0].student_id;
    
    	curr_student.grade = math[i].grade;
    	curr_student.last_name = math[i].last_name;
        curr_student.student_id = math[i].student_id;
    	
    	for (i = 0; i < CLASS_SIZE; ++i)
    	{
    		if (strcmp(curr_student.student_id, math[i].student_id))
    		{
    			if (math[i].grade == (('A' || 'B') || ('C' || 'D')))
    			{
    			if (math[i].grade == 'A')
    				stdt_total+=4;
    			else if (math[i].grade == 'B') 
    				stdt_total+=3;
    			else if (math[i].grade == 'C') 
    				stdt_total+=2;
    			else if (math[i].grade == 'D')
    				stdt_total+=1;
    			else if (math[i].grade == 'F')
    				stdt_total+=0;
    			else
    				printf("Error");
    			}
    
    				assignments++;
    
    				stdt_avg = (stdt_total/assignments);
    				printf("%s (Student ID #: %d) average for this 
    
    math class is %f\n",
    					curr_student.last_name, 
    
    curr_student.student_id, stdt_avg);
    
    		}
    		else
    		{
    			curr_student.last_name = math[i].last_name;
    			curr_student.student_id = math[i].student_id;
    			stdt_total = math[i].grade;
    			assignments = 0;
    		}
    	}
    	printf("\n\n%s",
    		"  Grade Scale: 3-4 = A\n"
    		"               2-3 = B\n"
    		"               1-2 = C\n"
    		"               0-1 = D\n");
    }
    
    int main(void)
    {
    	struct student  temp, math[CLASS_SIZE];         //, *p = &math;
    
    	math[0].grade = 'B';       // an array pre-filled with various students' 
    
    grades
        math[0].last_name = "Seinfeld";
        math[0].student_id = 001;
    
        math[1].grade = 'A';
        math[1].last_name = "Bond";
        math[1].student_id = 007;
    
        math[2].grade = 'C';
        math[2].last_name = "Newman";
        math[2].student_id = 003;
    
        math[3].grade = 'B';
        math[3].last_name = "Seinfeld";
        math[3].student_id = 001;
    
        math[4].grade = 'B';
        math[4].last_name = "Bond";
        math[4].student_id = 007;
    
        math[5].grade = 'C';
        math[5].last_name = "Newman";
        math[5].student_id = 003;
    
        math[6].grade = 'C';
        math[6].last_name = "Seinfeld";
        math[6].student_id = 001;
    
        math[7].grade = 'C';
        math[7].last_name = "Bond";
        math[7].student_id = 007;
    
        math[8].grade = 'C';
        math[8].last_name = "Newman";
        math[8].student_id = 003;
    
    	stdt_avg(math);
        return 0;
    }

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Keep following through the errors.

    Realize that C doesn't automatically typedef structure types like C++ does. So student means nothing on its own.

    So focus here for the moment:
    Code:
    	student curr_student.grade = math[0].grade;
    	student curr_student.last_name = math[0].last_name;
    	student curr_student.student_id = math[0].student_id;
    Also, keep in mind that initialization is different from assignment.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    cur_student.student_id is an int. strcmp (AKA string compare) only compares strings.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    I made some changes over lunch today and got it to comile, but my logic isn't correct. I'm trying to get each students total average, yet its printing each time thru. do I need another loop?



    Code:
    cl_info.h
    // 12.6 just asks for a function and not an entire program.
    // I wrote an entire program to make sure the function worked,
    // but the program itself isn't very robust or logical in a
    // real world situation.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define CLASS_SIZE 9
    //CLASS_SIZE is the total number of grade entries
    
    
    struct student
    {
    	char *last_name;
    	int  student_id;
    	char grade;
    };
    
    
    
    
    **********
    stdt_avg.h
    **********
    
    
    /*Programming in C */
    
    #include "cl_info.h"
    
    void stdt_avg(struct student math[])
    {
        int i = 0;
    	double assignments = 0.0, stdt_total = 0.0;
    		/* # of assignments and Total of all grades */
    	double stdt_avg = 0.0;         /* a Students Average Grade */
    
    	student curr_student;
    	
    	for (i = 0; i < CLASS_SIZE; i++)
    	{
    	curr_student.grade = math[i].grade;
    	curr_student.last_name = math[i].last_name;
        	curr_student.student_id = math[i].student_id;
    
    
    //printf("printme1\n");
    
    printf("%i %i %i\n", curr_student.student_id, math[i].student_id, i);
    
    	if ((curr_student.student_id) == (math[i].student_id))
    		{
    printf("printme2\n");
    //			if (math[i].grade == (('A' || 'B') || ('C' || 'D')))
    //			{
    printf("%c\n", math[i].grade);
    
    			if (((math[i]).grade)== 'A')
    				stdt_total+=4;
    			else if (((math[i]).grade) == 'B') 
    				stdt_total+=3;
    			else if (((math[i]).grade) == 'C') 
    				stdt_total+=2;
    			else if (((math[i]).grade) == 'D')
    				stdt_total+=1;
    			else if (((math[i]).grade) == 'F')
    				stdt_total+=0;
    //			}
    //			else
    //				printf("Error");
    				assignments++;
    printf("%i\n", assignments);
    printf("%i\n", stdt_total);
    		}
    	else
    		{
    		
    		stdt_avg = (stdt_total/assignments);
    		printf("%s (Student ID #: %d) average for this math class is %f\n",
    			curr_student.last_name, curr_student.student_id, stdt_avg);
    
    
    			curr_student.last_name = math[i].last_name;
    			curr_student.student_id = math[i].student_id;
    			stdt_total = math[i].grade;
    			assignments = 0;
    		}
    	}
    	printf("\n\n%s",
    		"  Grade Scale: 3-4 = A\n"
    		"               2-3 = B\n"
    		"               1-2 = C\n"
    		"               0-1 = D\n");
    }
    
    int main(void)
    {
    	struct student  temp, math[CLASS_SIZE];   //, *p = &math;
    
        math[0].grade = 'B';       // an array pre-filled with various students' grades
        math[0].last_name = "Seinfeld";
        math[0].student_id = 001;
    
        math[1].grade = 'A';
        math[1].last_name = "Bond";
        math[1].student_id = 007;
    
        math[2].grade = 'C';
        math[2].last_name = "Newman";
        math[2].student_id = 003;
    
        math[3].grade = 'B';
        math[3].last_name = "Seinfeld";
        math[3].student_id = 001;
    
        math[4].grade = 'B';
        math[4].last_name = "Bond";
        math[4].student_id = 007;
    
        math[5].grade = 'C';
        math[5].last_name = "Newman";
        math[5].student_id = 003;
    
        math[6].grade = 'C';
        math[6].last_name = "Seinfeld";
        math[6].student_id = 001;
    
        math[7].grade = 'C';
        math[7].last_name = "Bond";
        math[7].student_id = 007;
    
        math[8].grade = 'C';
        math[8].last_name = "Newman";
        math[8].student_id = 003;
    
    	stdt_avg(math);
        return 0;
    }

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dave_Sinkula
    Realize that C doesn't automatically typedef structure types like C++ does. So student means nothing on its own.
    Code:
    	student curr_student;
    Use the correct specifier for a floating point value:
    Code:
             printf("%i\n", assignments);
             printf("%i\n", stdt_total);
    Then I can get it to compile cleanly.

    [edit]BTW, this is a bit shorter.
    Code:
    int main(void)
    {
       struct student  temp, math[CLASS_SIZE] =
       {
          {"Seinfeld", 1, 'B'},
          {"Bond",     7, 'A'},
          {"Newman",   3, 'C'},
          {"Seinfeld", 1, 'B'},
          {"Bond",     7, 'B'},
          {"Newman",   3, 'C'},
          {"Seinfeld", 1, 'C'},
          {"Bond",     7, 'C'},
          {"Newman",   3, 'C'},
       };
       stdt_avg(math);
       return 0;
    }
    Last edited by Dave_Sinkula; 12-06-2006 at 10:29 PM. Reason: ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Dave, I really appreciate your time and patience with me while I worked this problem. I have it working (as crude as it is) so I'll sign off and maybe catch you on here again sometime. thanks again, regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM