Thread: struct array not showing integer variable

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    Question struct array not showing integer variable

    Greetings everyone, I am a newbie to C and eager to learn.
    Below is a snippet and reworded version of my problem, the scanf entry for my arrayone[] integer values are not being written to the text file. Everything else in the code is pretty much working except that bug. Is the snippet below sufficient for me to get some insight?

    regards.

    Code:
    void loadgame(struct gamerecord arrayone[], int size)
    {
        int i;
    
    
        gm = fopen("gameinformation.txt", "a");
    
    
        if (gm != NULL)
        {
            for (i = 0; i < size; ++i)
            {
                printf("\nEnter game name: ");
                scanf("%s", arrayone[i].title);
                printf("Enter The sequence number :");
                scanf("%d", arrayone[i].sequence);
                printf("Enter The list number :");
                scanf("%d", arrayone[i].list);
                
                
                //write items of data to the file
                fprintf(gm, "\n%s\t", arrayone[i].title);
                fprintf(gm, "%d\t", arrayone[i].sequence);
                fprintf(gm, "%d\t", arrayone[i].list);
                
    
    
            fclose(gm);
            }
        }

  2. #2
    Registered User
    Join Date
    Jan 2016
    Posts
    52
    Can you provide input/output examples and what does your struct look like?

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    Quote Originally Posted by jdodle View Post
    Can you provide input/output examples and what does your struct look like?
    __________________________________________________ _______
    This is an example of the output from the text file (from my actual project)

    title first last 3733720 3733724 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̈”Øðù8

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    Code:
    struct gamerecord
    {
        char title[50];
        int sequence[1];
        int list[3];
    }arrayone[];

    The previous post contains a few output not in the struct, (I changed the code to something simple, I dont want to post the full code online) I only have issues with the integer component that is giving me "3733720 3733724 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̈”Øðù8" as values. I hope that is not an issue.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should pay attention to your use of printf/scanf formats.

    $ gcc -Wall bar.c
    bar.c: In function ‘loadgame’:
    bar.c:31:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]
    bar.c:32:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]

    Why are sequence and list arrays of int, when you only read one int into each?
    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.

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    int array issue

    Quote Originally Posted by Salem View Post
    You should pay attention to your use of printf/scanf formats.

    $ gcc -Wall bar.c
    bar.c: In function ‘loadgame’:
    bar.c:31:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]
    bar.c:32:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]

    Why are sequence and list arrays of int, when you only read one int into each?

    I made sequence and list into an array, in order to use arrayone[] as a for loop, without list and sequence being an array, how would I be able to use it in a for loop to capture only one entry? I would use a while loop if it works better.

    the requirement of the project says I should use an array of structures. I thought everything should be an array. Forgive me if I am in error.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    Quote Originally Posted by Salem View Post
    You should pay attention to your use of printf/scanf formats.

    $ gcc -Wall bar.c
    bar.c: In function ‘loadgame’:
    bar.c:31:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]
    bar.c:32:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]

    Why are sequence and list arrays of int, when you only read one int into each?
    Why are sequence and list arrays of int, when you only read one int into each?

    THANK YOU!!!!!!!!!!!!

    I was using duplicated arrays. I removed the array from within my struct and it works!!!!

    I am still getting garbage after the next entry but I will fix that

    output looks like this now

    "Title FIRST LAST 1 123 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ®Bfì@÷D"

    I will figure out the "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ®Bfì@÷D"... I hope

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    struct array solved

    I realized I wasnt allocating enough space in my array to hold my string.

    That resulted in my garbage output.

    I allocated the space and now this part of my program runs as it should.

    Now I shall move on to the other areas. Thank for prompting me to think, review and coming to a conclusion. That knowledge is invaluable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof struct with variable length array?
    By MOS-6581 in forum C Programming
    Replies: 5
    Last Post: 03-16-2015, 09:16 AM
  2. Replies: 20
    Last Post: 07-09-2014, 02:48 AM
  3. Replies: 5
    Last Post: 09-22-2012, 01:35 PM
  4. GDB Not Showing Variable value
    By nickman in forum Linux Programming
    Replies: 5
    Last Post: 09-21-2012, 04:43 AM
  5. c++ not showing a variable
    By Mythic Fr0st in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2007, 11:05 PM

Tags for this Thread