calling struct class within array

This is a discussion on calling struct class within array within the C Programming forums, part of the General Programming Boards category; Hello i have a problem calling the struct class data from inside an array... error: C2664: 'DrukStudent' : cannot convert ...

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

    calling struct class within array

    Hello

    i have a problem calling the struct class data from inside an array...
    error: C2664: 'DrukStudent' : cannot convert parameter 1 from 'int' to 'student'.

    Code:
    struct student
    {
    	char naam[25];
    	struct datum gebdatum;
    	int resultaat;
    };
    
    void DrukStudent(struct student s)
    {	/* naam en gebdatum (dd/mm/jjjj) afdrukken */
    	printf("%-10s",s.naam); 
    	DrukDatum(s.gebdatum);
    	printf(" %d%%",s.resultaat);
    }
    
    double gemiddelde(struct student studentrij[ ], int aantal)
    {
    	int i;
    	double result=0.0;
    	printf("\ntest array\n");
    	DrukStudent(studentrij[1].resultaat); /* here is the problem */
    	return result;
    }
    
    void main()
    {
    	struct student studentrij[20] = {{"Anton",{25,4,1988}, 75},{"Bert",{24,5,1987}, 85},{"Chris",{23,6,1986}, 95},{"Dave",{22,7,1985}, 100}};
    	gemiddelde(studentrij,4);
    }
    My purpose is to calculate with the int resultaat within all these arrays.
    And i cant seem to target them.

    I do not wish to use any pointers (!!)

    I know that my double gemiddelde(struct student studentrij[ ], int aantal) isnt looking like it is supposed to be, but right now i just want to target the class of resultaat and then i can move on.



    Thanks and happy new year

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    DrukStudent expects a Student struct, but you pass an int.
    And main shall return int, always.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    thanks Elysia, but i cant seem to figure out how to target 85 only.
    since

    Code:
    DrukStudent(studentrij[1]);
    will give

    Code:
    Bert      24/05/1987       85%
    I dont need the name and date , but just the last int.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    So why are you printing the name and the date inside the function?
    Remove those printfs, change the function to take the score only and then just print the score.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    Quote Originally Posted by Elysia View Post
    So why are you printing the name and the date inside the function?
    Remove those printfs, change the function to take the score only and then just print the score.
    it would work then with

    Code:
    void DrukStudentGemiddelde(struct student s)
    {	/* naam en gebdatum (dd/mm/jjjj) afdrukken */
    	printf(" %d",s.resultaat);
    }
    but my point of learning to use struct was trying to target the score by calling it, without making new functions and structs or arrays etc ...

    if there is rly no other way ....

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Except what you have is targeting the score by calling it, without any extra structs or arrays. How is it not what you want?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If I understand this correctly, I'd use a while loop to move the index array from 20(minus 1, so 19 really), back to the comma. The first number would be your one's column digit, so no problem. Your second number, if present, would need to be X 10 + one's column digit, for your total number.

    Or, another way, just let the index decrement back to the comma, then reverse it and read in the digits. If you read in 3 digits, you'll need to do this:

    Correct value = digit1 * 100 + digit2 * 10 + digit3

    to get the value right.

    Code:
    i = 19;
    while(structArray[i] != ',')  //moving index back to the comma
       --i;
    
    for(j = 0; i < 20; i++)   {
       if(structArray[i] >= 0 && structArray[i] <= 9)  {  
          numberchar[j++] = structArray[i];
       }
    }
    
    for(i = j - 1; i < MaxChars (2 or 3); i++)   {
       temp = numberchar + '0';  //elevate char to a number
       if(i = 3)                 //restore it's 10's place, again.
          sum = temp * 100;
       else if(i = 2)
          sum += temp * 10;
       else
          sum += temp;
    }
    print your sum!

    I'm assuming 3 or fewer digits in this sample, and have not run this. It would be my starting place, however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Replies: 4
    Last Post: 12-12-2002, 02:32 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