Thread: problem with struct

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    problem with struct

    Hi,

    I would like to know how to do something like that:
    Code:
    scanf ("%d",iNoEmploye);
    &TabEmploye[iNoEmploye].iNo = iNoEmploye;
    Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nevrax View Post
    Hi,

    I would like to know how to do something like that:
    Code:
    scanf ("%d",iNoEmploye);
    &TabEmploye[iNoEmploye].iNo = iNoEmploye;
    Thanks
    First of all, you need an '&' in front of iNoEmploye in the call to scanf(). Second, you DON'T need an '&' on the next line.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Thanks, now I want to print a structure in a file without overwriting it (because it's information about different person and I want them all in the file.)
    Code:
       typedef struct {
           int iNo;
           char sNas[9];
           char sPrenom[30];
           char sNom[30];
           char cSexe;
           char sFin[3];
    }EMPLOYE;
    
    
    EMPLOYE TabEmploye[99];
    Thanks

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK, open a text file for writing and let fprintf() write out the information line by line.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    This is what i'm trying to do but unfortunatly, with a structure, I am not able to do something right. I would help me if I could have an example please.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    How about you post your attempt first?

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    this
    Code:
     void AjouterEmploye(void)
    /*----------------------------------------------------------------------------*/
    {
       clrscr();   
       gotoxy(20,23);
    
       int iNoEmploye;   
       typedef struct {
           int iNo;
           char sNas[9];
           char sPrenom[30];
           char sNom[30];
           char cSexe;
           char sAdresse[30];
           char sVille[30];
           char cEtatCivil;         
           int iJourNais;
           int iMoisNais;
           int iAnNais;   
           char sProv[30];
           char sCodPost[6];
           float fTauxHre;
           float fExempFed;
           float fExempPro; 
           char sCode[3];
           char sPrem[3];
           char sFin[3];
    }EMPLOYE;
    
    
    
    {     
       FILE * pFichier;    //pour la lecture du fichier,
       int i;
       int iValide;
       EMPLOYE TabEmploye[99];
       clrscr(); 
       time_t rawtime;
       time ( &rawtime );
       gotoxy(57,1);
       printf ( "%s", ctime (&rawtime) );  
       gotoxy(1,1);
       puts ("(2.2)");
       gotoxy(23,1);
       puts (" Le Roi de L'informatique Inc. ");
       gotoxy(32,2);
       puts ("Systeme de paye");
       gotoxy(31,3);
       puts ("Ajouter un employe");
       gotoxy(6,5);
       puts ("Numero de l'employe:");
       gotoxy(47,7);
       puts ("NAS:");
       gotoxy(2,9);
       puts ("Nom:");
       gotoxy(21,9);
       puts ("Prenom:");
       gotoxy(47,9);
       puts ("Sexe(F/M):");
       gotoxy(59,9);
       puts ("Etat civil(I/F):");
       gotoxy(67,10);
       puts ("AAAA MM JJ");
       gotoxy(2,11);
       puts ("Adresse:");
       gotoxy(47,11);
       puts ("Date de naissance:");
       gotoxy(2,13);
       puts ("Ville:");
       gotoxy(24,13);
       puts ("Prov.:");
       gotoxy(47,13);
       puts ("Taux horaire:");     
       gotoxy(2,15);
       puts ("Code postal:");
       gotoxy(24,15);
       puts ("Telephone:(   )   -");
       gotoxy(2,17);
       puts ("Exemption federal:");
       gotoxy(47,17);
       puts ("Taux horraire:");
       
    
       fflush(stdin); 
       gotoxy(26,5);
       scanf ("%d",&iNoEmploye);
       i=iNoEmploye;
       
       TabEmploye[i].iNo = iNoEmploye; 
       gotoxy(51,7);
       scanf("%d",TabEmploye[i].sNas);      
       gotoxy(6,9);
       scanf("%s",TabEmploye[i].sNom);   
       gotoxy(28,9);
       scanf("%s",TabEmploye[i].sPrenom);   
       gotoxy(57,9);   
       scanf("%s",&TabEmploye[i].cSexe);   
       gotoxy(75,9);   
       scanf("%s",&TabEmploye[i].cEtatCivil);    
       fflush(stdin);
       gotoxy(10,11);   
       gets (TabEmploye[i].sAdresse);  
       gotoxy(67,11);
       scanf("%d",&TabEmploye[i].iAnNais);   
       gotoxy(72,11);
       scanf("%d",&TabEmploye[i].iMoisNais);   
       gotoxy(75,11);
       scanf("%d",&TabEmploye[i].iJourNais);  
       fflush(stdin);
       gotoxy(8,13);
       gets (TabEmploye[i].sVille);   
       fflush(stdin);      
       gotoxy(30,13);
       gets (TabEmploye[i].sProv);   
       gotoxy(60,13);
       scanf("%f",&TabEmploye[i].fTauxHre);   
       gotoxy(14,15);
       scanf("%f",&TabEmploye[i].sCodPost);    
       fflush(stdin);
       gotoxy(35,15);
       scanf("%d",&TabEmploye[i].sCode);
       gotoxy(39,15);
       scanf("%d",&TabEmploye[i].sPrem);
       gotoxy(43,15);
       scanf("%d",&TabEmploye[i].sFin);
       gotoxy(20,17);
       scanf("%d",&TabEmploye[i].fExempFed);
       gotoxy(61,17);
       scanf("%d",&TabEmploye[i].fExempPro);
    
       pFichier = fopen("EMPLOYE.DAT", "w");
       fputs (TabEmploye[i]);    
       CalculerPaye();
       }
    }

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    fputs needs the file that you're sending the string to. Something like:

    Code:
    fputs(TabEmployee[i],pFichier);
    BTW, for future reference, don't use non-standard functions if you don't have to.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    you're right, I've posted to fast. With
    Code:
    fputs(TabEmployee[i],pFichier);
    I got the following message
    Code:
    539 H:\420-2pf\Projet.cpp cannot convert ` AjouterEmploye()::EMPLOYE' to `const char*' for argument `1' to `int
    And for the non-standart library, I totally agree with you, but since in work in command windows, I don't really have the choice yo use non standard library.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > gotoxy(43,15);
    > scanf("%d",&TabEmploye[i].sFin);
    Remove all the gotoxy() stuff until you've finished the core logic of the problem.

    Until then, it's
    - a distraction for you as you keep editing the positions as you change the code.
    - useless to us as many can't even compile your non standard code.

    When everything works, then you can spend the last hour just putting in all the gotoxy() in the right places, and simply re-testing to make sure it still works.

    Also, it was only necessary to have a couple of fields in the struct to prove the point, not 20 of them.

    > fflush(stdin);
    See the FAQ
    Then use fgets() + sscanf() - or better yet, one of strtod() or strtol()
    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.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Ok, thanks for the info. Unfortunatly, I don't really know what's non-stantard (except what you're telling me)since my teacher know nothing of C and she dosn't care at all about standard program. I trully appreciate your help by the way.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    remove conio.h if you have in your code and see what functions will be noted in warnings saying gotoxy has no prototype... commant this functions out... (has some side effect - if you forget to include some standard header for any other standard function you use)

    or find some online C-reference and check each function you use - there.
    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

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by vart View Post
    remove conio.h if you have in your code and see what functions will be noted in warnings saying gotoxy has no prototype... commant this functions out... (has some side effect - if you forget to include some standard header for any other standard function you use)

    or find some online C-reference and check each function you use - there.
    Well, most compilers would warn you about unprototyped functions, but I'm not sure that whatever the OP's using to compile that code would.

    Your non-standard functions are gotoxy() and clrscr(). And fflush(stdin) as well as gets() are both very bad ideas.

    If a function you use is not on this list, it's probably not standard: http://cppreference.com/all_c_functions.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM