Thread: Input for Struct?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    18

    Input for Struct?

    I have this assignment and am trying to just first get my input correct. I can't seem to get the int input right. I posted earlier and have read the FAQs, but can't seem to get it. I keep getting:

    a8.c: In function `main':
    a8.c:22: warning: int format, different type arg (arg 2)

    Here's my code so far:

    Code:
    #include <stdio.h>
    
            struct info_struct {
                    short int id;
                    char *name;
                    } members[10];
    
    int main (void)
    {
    
            int i, j;
    
            for (i=0; i<10; i++)
                    {
                    printf("Please enter name: ");
                    gets(members[i].name);
                    printf("Please enter ID:   ");
                    scanf("%d", &members[i].id);
                    }
    
            for (j=0; j<10; j++)
                    {
                    printf("%d, %s\n", members[j].id, members[j].name);
                    }
    
            return 0;
    
    }
    Any help would...help. Thanks.

  2. #2

    Post

    Hello stewade,

    From what I tried is I changed:
    gets(members[i].name);
    To:
    char buffer[256];

    fgets(buffer, sizeof(buffer), stdin);
    members[i].name = buffer;


    The thing is with char *name, you have to allocate memory, but you cant do it very well esp if the data was just inputted. Also gets() isn't wise to use all the time due to its poor design and buffer overflow problems. It doesn't allow you to set a max before the string truncates, thats why I used fgets(), which allows you to set the size of what you want to copy, likewise sizeof(buffer). Then after that I set members[i].name to equal buffer, not needing any allocation.

    Remember, stdin is the data inputted into the stream which we read and set to buffer.

    Hope this helps,
    - Stack Overflow
    Last edited by Stack Overflow; 05-02-2004 at 11:08 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    I think that solves my name input, but the problem when it comes to compiling involves my "int" input - the ID number. I've been able to input the name, but I can't get the other to work. Any ideas on that one? By the way, thanks for the help.

  4. #4

    Post

    Alright,

    I did notice that main() was intialized with a void call inside. I'm not sure if this will remedy your error, but this is usually how I see them:

    int main() {

    }


    Or:

    int main(int argc, char *argv[]) {

    }


    Hope that helps, also your program ran fine on my compiler, and I got all the ID numbers including Names printed at the end of the 10th input. scanf("%d"... should work, you could always try %i instead, not sure if that will change much. If not, is it giving you any errors with the struct, or is it just showing blank info?

    Edit: Also, you could try changing short int to int or short.

    - Stack Oerflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    The error I get from the compiler is:

    a8.c: In function `main':
    a8.c:25: warning: int format, different type arg (arg 2)

  6. #6

    Post

    Well,

    I did some searching around and can't seem to come across any information of how to fix this. Does changing the data type short int to int for id still compile with the same error? If not, I'm still looking why a compiler should give this error.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Wahoo! I changed it to a regular "int" and it worked. Thanks man. I have another part to the problem I have to figure out, so I am going to try and figure that out...you never know, I could be back with more problemos. Thanks again.

  8. #8

    Smile

    Yeah,

    Thats good news! Glad all the little compiler issues are out of the way now Good luck with the rest, don't hesitate to come back and ask more questions and I was glad to be of help.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Ok, I've run into another compiler error, most likely because of sheer stupidity on my part. The compiler error reads:

    a8.c: In function `main':
    a8.c:33: error: request for member `id' in something not a structure or union

    I have to sort the names and Id numbers according to ID numbers using this function provided. I am not quite sure how to pass the parameters...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
            struct info_struct {
                    int id;
                    char *name;
                    } members[10];
    
    void bubbleSort(int numbers[], int array_size);
    
    int main ()
    {
    
            int i, j, k=10;
    
            for (i=0; i<10; i++)
                    {
                    char buffer[256];
    
                    fflush(stdin);
                    printf("Please enter name: ");
                    fgets(buffer, sizeof(buffer), stdin);
                    members[i].name = buffer;
    
                    printf("Please enter ID:   ");
                    scanf("%i", &members[i].id);
                    }
    
                    bubbleSort(members.id, k);
    
            for (j=0; j<10; j++)
                    {
                    printf("%d, %s", members[j].id, members[j].name);
                    }
    
            return 0;
    
    }
    
    void bubbleSort(int numbers[], int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    }
    I have tried these 2 as well:

    Code:
    bubbleSort(members, k);
    and

    Code:
    bubbleSort(members[], k);
    Any clue on how to fix my parameter pass?

  10. #10
    Ok,

    This is simple. Make a new integer calle l the lower case L, or whatever letter you like:

    Code:
    int i, j, k=10, l;
    int numbers[11]; // make 11 in case the last array is used for null terminator (just in case)
    
    for (l=0; l<k+1; l++) { // less than 11 stops at 10
    	numbers[l] = members[l].id;
    }
    
    bubbleSort(numbers, k+1); // bubble sort does (array_size - 1) - 10 - 1 = 9, so 11 - 1 = 10
    That should solve your problem, I hope.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Ok, this is what I put in for my input:

    Code:
    Please enter name: a
    Please enter ID:   1
    Please enter name: b
    Please enter ID:   2
    Please enter name: c
    Please enter ID:   3
    Please enter name: d
    Please enter ID:   5
    Please enter name: e
    Please enter ID:   4
    Please enter name: f
    Please enter ID:   9
    Please enter name: g
    Please enter ID:   8
    Please enter name: h
    Please enter ID:   7
    Please enter name: i
    Please enter ID:   6
    Please enter name: stew
    Please enter ID:   100
    and here is what came out for the output:

    Code:
    1, stew
    2, stew
    3, stew
    5, stew
    4, stew
    9, stew
    8, stew
    7, stew
    6, stew
    100, stew
    I have gone through the code and am really getting lost as to what exactly is happening in the bubblesort.

    And just for reference, here is my whole code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
            struct info_struct {
                    int id;
                    char *name;
                    } members[10];
    
    void bubbleSort(int numbers[], int array_size);
    
    int main ()
    {
    
            int i, j, k=10, l;
            int numbers[11];
    
            for (i=0; i<10; i++)
                    {
                    char buffer[256];
    
                    fflush(stdin);
                    printf("Please enter name: ");
                    fgets(buffer, sizeof(buffer), stdin);
                    members[i].name = buffer;
    
                    printf("Please enter ID:   ");
                    scanf("%i", &members[i].id);
                    }
    
                    for (l=0; l<(k+1); l++)
                    {
                    numbers[l] = members[l].id;
                    }
    
                    bubbleSort(numbers, k+1);
    
            for (j=0; j<10; j++)
                    {
                    printf("%d, %s", members[j].id, members[j].name);
                    }
    
            return 0;
    
    }
    
    
    void bubbleSort(int numbers[], int array_size)
    {
      int i, j, temp;
    
      for (i = (array_size - 1); i >= 0; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if (numbers[j-1] > numbers[j])
          {
            temp = numbers[j-1];
            numbers[j-1] = numbers[j];
            numbers[j] = temp;
          }
        }
      }
    }
    This stuff frustrates me so much for some reason. Yugh...thanks so much for your help, I really appreciate it.

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The error you were having was due to the fact you were using the %d flag in scanf when you needed to use the %h flag..which scans a short int.

  13. #13
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    char buffer[256];
    
    fflush(stdin); /* undefined behavior, please consult the faq */
    printf("Please enter name: ");
    fgets(buffer, sizeof(buffer), stdin);
    members[i].name = buffer;
    There are a couple things wrong here, first of all fflush() is undefined when operating on input streams.
    Secondly you read in the name correctly, but unfortunately all youre doing with members[i].name is POINTING it at buffer, you dont store the buffer in it....so every members.name element ends up pointing to buffer (which gets overwritten every time except for the last time, hence why all the names appear to be stew, because every element is pointing to the buffer ) The solution is to allocate memory and point members[i].name at that, then strcpy buffer into the memory.

    Code:
    /* one way, not the safest, should check for error on malloc */
    members[i].name = malloc (sizeof buffer);
    strcpy(members[i].name,buffer);
    
    /* another commonly used method, again should check for NULL */
    members[i].name = strdup(buffer); /* requires string.h */
    Hope that helps.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Thank you very much, that definitely solved my name input; however, it doesn't seem like the bubblesort is even working for some reason. My input is this:

    Code:
    Please enter name: a
    Please enter ID:   9
    Please enter name: b
    Please enter ID:   8
    Please enter name: c
    Please enter ID:   7
    Please enter name: d
    Please enter ID:   6
    Please enter name: e
    Please enter ID:   5
    Please enter name: f
    Please enter ID:   4
    Please enter name: g
    Please enter ID:   3
    Please enter name: h
    Please enter ID:   2
    Please enter name: i
    Please enter ID:   1
    Please enter name: j
    Please enter ID:   22
    And my output is this:

    Code:
    9, a
    8, b
    7, c
    6, d
    5, e
    4, f
    3, g
    2, h
    1, i
    22, j
    When it SHOULD be this after bubblesort:

    Code:
    1, i
    2, h
    3, g
    4, f
    5, e
    6, d
    7, c
    8, b
    9, a
    22, j
    See how it is supposed to sort the ID's by number? I assume I am passing the parameters in a way that isn't allowing the address to be changed or something of that nature, but can't seem to make it work without getting a compile error?????

  15. #15

    Post

    Hi stewade,

    Sorry for the long delay, I had to get some sleep sooner or later

    So I think I found the answer. Use an index per member[] to know what name/id corresponds with the following member call. Also, add your own addition to the bubbleSort() to re-sort your indecies too:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    struct info_struct {
    	int id;
    	int index;
    	char *name;
    } members[10];
    
    void bubbleSort(int numbers[], int array_size);
    
    int main(int argc, char *argv[]) {
    	int i, j, l, k=10;
    	int numbers[11];
    	char buffer[256];
    
    	for (i = 0; i < k; i++) {
    		fflush(stdin);	// flush existing input
    		printf("Please enter name: ");
    		fgets(buffer, sizeof(buffer), stdin);
    		members[i].name = strdup(buffer);	// set name to buffer
    		printf("Please enter ID:   ");
    		scanf("%d", &members[i].id);
    		members[i].index = i;	// store index
    	}
    
    	for (l = 0; l < k; l++) {
    		numbers[l] = members[l].id;
    	}
    
    	bubbleSort(numbers, k);
    
    	// (members[j].index) is the index we need
    	for (j = 0; j < k; j++) {
    		printf("%d, %s\n", numbers[j], members[members[j].index].name);
    	}
    
    	return 0;
    }
    
    void bubbleSort(int numbers[], int array_size) {
    	int i, j, temp;
    
    	for (i = (array_size - 1); i >= 0; i--) {
    		for (j = 1; j <= i; j++) {
    			if (numbers[j-1] > numbers[j]) {
    				temp = numbers[j-1];
    				numbers[j-1] = numbers[j];
    				numbers[j] = temp;
    
    				// Our addition (resort index)
    				members[j-1].index = j;
    				members[j].index = j-1;
    			}
    		}
    	}
    }
    Code 1.1: Sort ID corresponding with Name

    Most of the code I have explained here. If you still need the short int you can try %h as nonpuz stated. Also, as nonpuz stated, I implemented strdup() on each member name so the data wouldn't get overwritten. I also put comments by the newest additions to the code so you can see what and how I did it.

    If you have further questions, feel free to ask.


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM