inputting data in a struct

This is a discussion on inputting data in a struct within the C Programming forums, part of the General Programming Boards category; i have some problems inputting data in a struct Code: #include <stdio.h> #include <unistd.h> void input(patient *p, int nr, char ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    inputting data in a struct

    i have some problems inputting data in a struct


    Code:
    #include <stdio.h>
    
    #include <unistd.h>
    void input(patient *p, int nr, char cpr, char ankomst, char skade); 
    
    typedef struct patient 
    {
    	int nr;
    	char cpr[10];
    	char ankomst;
    	char skade;
    } patient;
    patient p[1000]; 
    int patientnr = 0;
    
    int main()
    {
    
            char tempCpr[10] = "02020202";
    	char tempAnkomst[1] =  "A"; 
    	char tempSkade[1] = "B"; 
    
    input(&p[patientnr], tempCpr, tempAnkomst, tempSkade);
    
    }
    
    void input(patient *p, int nr, char cpr, char ankomst, char skade)
    {
    	p->nr = nr;
    	p->cpr = cpr;
    	p->ankomst = ankomst2;
    	p->skade = skade;
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    forgot to add something.. read this code instead

    Code:
    #include <stdio.h>
    
    #include <unistd.h>
    void input(patient *p, int nr, char cpr, char ankomst, char skade); 
    
    typedef struct patient 
    {
    	int nr;
    	char cpr[10];
    	char ankomst;
    	char skade;
    } patient;
    patient p[1000]; 
    int patientnr = 0;
    void get(patient *p);
    
    int main()
    {
    
            char tempCpr[10] = "02020202";
    	char tempAnkomst[1] =  "A"; 
    	char tempSkade[1] = "B"; 
    
    input(&p[patientnr], tempCpr, tempAnkomst, tempSkade);
    get(&p[patientnr]);
    
    }
    
    void input(patient *p, int nr, char cpr, char ankomst, char skade)
    {
    	p->nr = nr;
    	p->cpr = cpr;
    	p->ankomst = ankomst2;
    	p->skade = skade;
    }
    
    void get(patient *p)
    {	
    	
    
    	
    	char cpr[10];
    	char ankomst3[1];
    	char skade2[1];
    	cpr = p->cpr;
    	ankomst3 = p->ankomst;
    	skade2 = p->skade;
    	
    	printf("\n %c\n", cpr);
    	printf("\n %c\n", ankomst3);
    	printf("\n %s\n", skade2);
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    it says incompatible types in assignment
    for these three lines
    cpr = p->cpr;
    ankomst3 = p->ankomst;
    skade2 = p->skade;

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    there's an awful lot wrong with that code, including some really
    fundamental errors regarding use of strings and structs. i
    suggest that youstudy the tutorial and make sure you
    understand what you are doing before you try to write the
    complete program. i would scrap that code and start afresh
    instead, because trying to fix it will probably introduce more
    errors.

    http://www.cprogramming.com/tutorial/c/lesson7.html
    Last edited by Richie T; 02-01-2006 at 06:46 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, i have seen this type code use by many people.
    Code:
    char tempAnkomst[1] =  "A"; 
    char tempSkade[1] = "B";
    why do u need a string to just store a one char. why cant u declare just a single char varibale. instead of declaring string and wasting the memeory and getting the error invalid assignmnet. the whole programe what u have done is just fine. rather than this part. i strongly recommand people not to use this type of method to just store a char. if this is method thought by the lecture just ignore it. instead use this

    Code:
    char tempAnkomst = 'A'; 
    char tempSkade = 'B';
    in your case raja. u have done it right in the skeleton of your struct. but atleast from that, u could have understood what the error is. your struct
    Code:
    int nr;
    char cpr[10];
    char ankomst;
    char skade;
    } patient;
    is the bolded code is string or a char. if its char how can u assign a char pointer to a charr.

    think about that.

    ssharish2005

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    thanks for the tutorial..it really helped.. its working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Data struct question
    By ronenk in forum C Programming
    Replies: 4
    Last Post: 03-05-2005, 08:41 AM

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