Thread: Struct Help

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    Struct Help

    Hi....i want to add data into a struct array and save it. how will i do this?i can get it to work.please help....


    Code:
    // STRUCTURES
    
    struct CatRec {
       char catname[10];  // Cat Name
       char gender;       // The Gender of the Cat: M,F
       char type[20];     // Type of Cat
       char color[10];    // The Color of the Cat
       int  age;          // Age of Cat
       char genuine;      // Genuine Breed
    };
    
    
    void main()
    {
      struct CatRec Cats[100];
      int choice;
      int n=1;
    
      do
      {
           clrscr();
    
           choice = Menu(choice);
    
            switch (choice)
            {
                    case 1: clrscr();
                            AddCat(Cats,n);
                            break;
    
    ............... bla bla bla
    
    
    void AddCat(struct CatRec Cats[],int &n)
    {
        char choice;
    
    
          cprintf("Cat Name: ");
          cin>>Cats[n].catname;
    
    
          cprintf("\nGendar(M/F): ");
          cin>>Cats[n].gender;
    
    
          cprintf("\nBreed Type: ");
          cin>>Cats[n].type;
    
    
          cprintf("\nCat Color: ");
          cin>>Cats[n].color;
    
    
          cprintf("\nAge: ");
          cin>>Cats[n].age;
    
          cprintf("\nGenuine Breed(Y/N): ");
          cin>>Cats[n].genuine;
    
          cprintf("SAVE CHANGES(Y/N): ");
          cin>>choice;
    
          if (toupper(choice) == 'Y')
                printf("SAVED");
          else
               printf("NOT SAVED");
    
          getch();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, you read this, and fix your problem. Then you read this, and give it a try.

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Can you describe what isn't working, error messages, whatever?

    do you have #include <iostream>, for an up to date compiler, or #include <iostream.h>, if your compiler doesn't use up to date standard headers, so you can use cin >>?

    I would argue against mixining C and C++ type I/O in the same program, unless you have a good handle on both types of I/O routines.

    In case you're interested: Using C++ structs you don't need the keyword struct in the following lines:

    struct CatRec Cats[100];
    void AddCat(struct CatRec Cats[],int &n)

    which I find helpful (less typing for one thing and C++ structs are more flexible than C structs).

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C++ structs are more flexible than C structs
    Really, how so?
    Code:
    struct foo
    {
        int x;
    };
    How is that any different in C++ than it is in C?

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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Really, how so?
    Code:
    struct node {
      int data;
      node *left, *right;
    
      node ( int init, node *llink, node *rlink )
        : data ( init ), left ( llink ), right ( rlink )
      {}
    };
    'Tis more convenient than the equivalent C method (pun intended). Not to mention that C++ structures are exactly the same as classes with the exception of default access rights. But I doubt that is what elad was talking about. He probably meant the automagic typedef so that the struct keyword could be omitted. In that I agree that it's a superfluous feature.
    My best code is written with the delete key.

  6. #6
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    in two posts made by prelude i noticed initializing an int by the means of of ( )
    data ( init )
    is that kinda like a constructor? does it set the value of it without using =?
    sorry if its a newbish question, but i havent really seen it much.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is that kinda like a constructor?
    Kinda, yes.

    >does it set the value of it without using =?
    Yes, but only on initialization. These two are equivalent:
    Code:
    int i = 0;
    int j ( 0 );
    But these two are not:
    Code:
    i = 10;
    j ( 20 );
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM