Thread: Looping struct info

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    Looping struct info

    Currently my code outputs to what I want it to do (print out everyone's given name) shown below:
    Code:
    #include<stdio.h>
    
    main()
    {
        struct acct
        {
    	int accountNumber;
    	char givenName[20];
    	char familyName[30];
    	char streetAddress[40];
    	char city[20];
    	char phoneNumber[12];
    	float balance;
        };
    
    struct allAccounts
        {
    	struct acct a000001;
    	struct acct a000002;
    	struct acct a000003;
    	struct acct a000004;
    	struct acct a000005;
    	struct acct a000006;
    	struct acct a000007;
    	struct acct a000008;
    	struct acct a000009;
    	struct acct a000010;
        }accounts=
            {
    	  {61500000, "Fred", "Gorf", "123 Shady Lane", "Kitchener","519 555 1212",  0.25},
    	  {61500001,"Bill", "Blass", "666 Dalmation Way", "Kitchener", "519 555 1213", 123456.78},
    	  {61500002,"Will", "Last", "356  SadyVille", "Kitchener", "519 555 1214", 1238},
    	  {61500003,"Bruno", "Caldas", "789  ShadyVille", "Kitchener", "519 555 1215", 1234},
    	  {61500004,"Joe", "Brook", "965  ShadyVille", "Kitchener", "519 555 1216", 3456},
    	  {61500005,"Fern", "Brown", "543  ShadyVille", "Kitchener", "519 555 1217", 56483},
    	  {61500006,"Spring", "Natural", "778  ShadyVille", "Kitchener", "519 555 1218", 6895},
    	  {61500007,"Water", "Mineral", "875  ShadyVille", "Kitchener", "519 555 1219",56359 },
    	  {61500008,"Paul", "Tim", "333  ShadyVille", "Kitchener", "519 555 1220",568419 },
              {61500009,"Sam", "Horton", "835  ShadyVille", "Kitchener", "519 555 1222",65823 }
    	};
    
    typedef struct acct ACC_TYPE;
        ACC_TYPE *ptr;
        ptr = &accounts.a000001;
    
    printf("%s\n", (*ptr).givenName);
    printf("%s\n", (*(ptr + 1)).givenName); 
    printf("%s\n", (*(ptr + 2)).givenName);
    printf("%s\n", (*(ptr + 3)).givenName);  
    printf("%s\n", (*(ptr + 4)).givenName);
    printf("%s\n", (*(ptr + 5)).givenName);
    printf("%s\n", (*(ptr + 6)).givenName);
    printf("%s\n", (*(ptr + 7)).givenName);
    printf("%s\n", (*(ptr + 8)).givenName);
    printf("%s\n", (*(ptr + 9)).givenName);
    
    
    }
    I tried making a loop like
    Code:
    int=1
    while (i=0,0<10,i++)
    printf("%s\n", (*(ptr + i)).givenName);
    hoping that could replace all those repetitive print statements, but I get some sort of casting error or something.. Was woundering if someone know how to change the loop statement to get it to work.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of making one struct with all the accounts listed inside it, you need to reverse it - make one array of structs, where each element of the array, holds one struct.

    Then you can access the records and their members, very easily in one loop.

    Code:
    for(i = 0; i < RecordNum; i++)
      printf("%d %s ", array[i].id, array[i].name);

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    my assignment gave an example and it had to be organized this way... is their no other way to loop it without reversing it? and your recordNum would be equivalent to accounts possibly?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kokoro View Post
    my assignment gave an example and it had to be organized this way... is their no other way to loop it without reversing it? and your recordNum would be equivalent to accounts possibly?
    Oh OK!

    I've never seen a program with that kind of data organization, before.

    this part of your program can be easily modified from this:

    Code:
    ptr = &accounts.a000001;
    
    printf("%s\n", (*ptr).givenName);
    printf("%s\n", (*(ptr + 1)).givenName); 
    printf("%s\n", (*(ptr + 2)).givenName);
    printf("%s\n", (*(ptr + 3)).givenName);  
    printf("%s\n", (*(ptr + 4)).givenName);
    printf("%s\n", (*(ptr + 5)).givenName);
    printf("%s\n", (*(ptr + 6)).givenName);
    printf("%s\n", (*(ptr + 7)).givenName);
    printf("%s\n", (*(ptr + 8)).givenName);
    printf("%s\n", (*(ptr + 9)).givenName);
    to this:
    Code:
    ptr = &accounts.a000001;
    
    for(i = 0; i < 10; i++)
       printf("%s\n", (*(ptr + i)).givenName);
    The recordNum would just be the number of records that you had, all together. If you didn't know,
    you might do your print out function loop using while() instead of for (although with a break statement,
    either could be used).

    Account number would be a field in the record of the struct:
    Code:
    struct Accounts {
       unsigned long accountNum;
       char[20] fname;
    //   etc.
    }
    Last edited by Adak; 11-25-2009 at 10:01 PM.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    so i just used the wrong loop... i used a while instead of a for loop... lol cant believe i didnt try that out before.. thanks!

    If i'm trying to get user to input the newest account I would make an empty string in my structs and then I make a function for user input like:
    Code:
    userInput (ACC_TYPE ptr) // bringing in my ptr value and need the acc cus its a deftype
    {
    
    printf("What is your first name?\n");
      scanf("%d",&(*ptr).givenName);
      strcpy(accounts.a000011.givenName, "");
    
    printf("what is your last name?\n");
      scanf("%d", &(*ptr).familyName);
      strcpy(accounts.a000011.familyName, "");
    
    }
    would my scanf and sctrcpy accurately take in user input and copy it into the string or is it doing something else? I cant find it out cus i get an error on my function line saying i need a ")" before ptr. My prof told me to try to reorganize my code and see if that works. (bringing my structs out of main function and what not. I'm going to try that but i dont think my scanf and strcpy are right

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, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Binary trees search problem...
    By Umoniel in forum C Programming
    Replies: 2
    Last Post: 02-22-2004, 02:29 PM