Thread: Basic C programming help needed

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    42

    Basic C programming help needed

    Well I just started my computer science I class and I need some help with structs. I pretty much failed structs in my intro to c class but did well on everything else so i made it out with a B.


    I've been learning them on my own the past few days and I understand structs like:
    Code:
        struct people {
               int age;
               int height;
               int weight;
               } person[5];
    
    
    //then defining them like:
    
    person[1].age = 12;
    person[1].height = 60;
    person[1].weight = 140;
    and so on. But for my program I need to use a character member that will at most take in a 19 character name and an integer member with 6 different numbers (for a lotto ticket)


    Here's how i have it as:



    /*num_people is the first line in the file we are getting the data from, is that correct usage of it?*/

    Code:
    struct people {
           char name[19];
           int  numbers[6];
           } person[num_people];
    
           person[1].name = "Llewellyn Mark";
           person[1].numbers = {1, 2, 3, 4, 5, 6};
    /* We need to take the names and numbers from a file but I first want to understand it from direct input which I don't yet because the above doesn't compile*/


    So basically what would the above look like done correctly? I haven't been able to figure it out.
    Last edited by DaNxTh3xMaNx; 08-28-2010 at 08:06 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to get the size of your array of structs, BEFORE you create the array.

    For now, perhaps just use a number like 5 or 10?

    If you put comments in your code, please keep the line length short. The font used for code is quite wide, and long lines "break" the forum tables, and make your posts hard to read.

    Better to put long comments on code, outside the code tags.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    .

    i take it you have defined num-people?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    Quote Originally Posted by rogster001 View Post
    i take it you have defined num-people?
    it was going to be scanned in from a file.

    Code:
    fscanf(ifp, "%d", &num_people);

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    num-people should be an integer, if you have assigned its value from reading a file then ok, if not hard code it or user input to set it, you may get problem as size unknown at compile time
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
           person[1].name = "Llewellyn Mark";
           person[1].numbers = {1, 2, 3, 4, 5, 6};
    You cannot just assign an array.

    Question 6.6b
    Arrays and Pointers

    Btw, Array index in C start at 0.
    And change num_people to NUM_PEOPLE if it's macro or enum.
    Last edited by Bayint Naung; 08-28-2010 at 08:11 AM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    well would any of you mind giving me a simple array of structures with directly inputted values and then prints them out?

    I still can't understand it from your posts but if i just saw an example (i dont need/want it to be with the info my program is using just a simple array of structures so i can see how it works and looks)

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Any help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Info{                //global definition of struct Info
      char name[81];  
      int phone;    
      char street[81];
      char email[81];
    };                          //no Info struct exists yet
    
    
    void print(struct Info info){
      printf("Name: %s\n", info.name);
      printf("Phone Number: %d\n", info.phone);
      printf("Street address: %s\n", info.street);
      printf("E-mail: %s\n", info.email);
    }
    
    
    int main(void){
    	
      int i;
      /* declare an instance of the Info struct and assign some values */
      struct Info info = {"Buzz Lightyear", 12345, "999 Infinity Way", "[email protected]" };
      struct Info *pinfo = &info;         //declare a pointer to struct
                                          //and assign it the address of info
      printf("\n\n\n\n\n");
    	print(info);	                      //print via struct members
    
      printf("\n\n");                     //print via pointer to struct members
      printf("Name: %s\n", pinfo->name);
      printf("Phone Number: %d\n", pinfo->phone);
      printf("Street address: %s\n", pinfo->street);
      printf("E-mail: %s\n", pinfo->email);
    	 
      printf("\n\n\t\t\t    press enter when ready");
      i=getchar(); ++i;
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    yes Adak that definitely helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Basic Help Needed
    By pobri19 in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2008, 04:57 AM
  2. Basic C-- Help Desperately Needed
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 02-14-2008, 11:08 PM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. VB vs. BASIC
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-05-2002, 08:55 PM
  5. DJGPP help needed
    By dune911 in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2001, 04:56 PM