Thread: 'request for member 'track' in something not a structure or untion

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    28

    'request for member 'track' in something not a structure or untion

    Trying to scanf to a struct, but when compiling, I am given: 'request for member 'track' in something not a structure or union.

    I have looked around on the internet and one suggestion was to replace the '.' with '->', which did not help. I have a feeling it is to do with where and how I have placed the struct, as example codes seem to place them in a different 'main'. Also, I am unsure if I should be entering an array number into the 'scanf' if there is an 'if statement' specifically for array entry.

    Thank you! I hope what I have asked makes sense.

    Here is the code, excluding the majority of the switch statement:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h>
    #include <windows.h>//for changing colour
    
    struct song //creates a struct called song
    {   
        char track[20];
        char trackname[20]; 
        char artist[20];
        char genre[20];
    };
    
    int main(void)
    
    {
        system("color f5");//gives the window a white background (f) and purple text (5)
        
        
        int menu, option, playchoice; 
        char song[20];
        
        printf("Make a selection\n\n");
        
        printf("1=Play\n");
        printf("2=Music\n");
        printf("3=Manage\n");
        printf("4=Four\n");
        printf("5=Quit\n");
        scanf("%d", &menu );
        
        switch( menu ) 
        
        {
                
    
            case 3: 
                
    
        int qtyadd;
        
        printf("Number of tracks to add: \n\n");
        scanf("%i", &qtyadd);  
        
        { 
    
                 printf("Filepath for track to add: \n\n");
                 scanf("%s", song[1].track]; //references to the first record and its first variable
                 printf("Name of track: \n\n");
                 scanf("%s", songs[1].trackname);
                 printf("Artist: \n\n");
                 scanf("%s", songs[1].artist);
                 printf("Genre: \n\n");
                 scanf("%s", songs[1].genre);
    } 
    for(i=0;i<qtyadd && i<99;i++) 
    
    
    struct song songs[99]; 
    int i; 
    for(i=0;i<qtyadd && i<99;i++) ;
                
                
            break;
    
        }
    
        		
    system("pause"); 
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
                scanf("%s", songs[1].track]; //references to the first record and its first variable
    But you have to define the array of song before you use it.
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Thank you, ZuK. The code stopped that problem. It now says: syntax error before ']' token.

    I am assuming that that is because you said I would have to define the array of song before I use it, which I am unsure about, but I changed the struct to:

    Code:
    struct songs[99] //creates a struct called song
    {   
        char track[20];
        char trackname[20]; 
        char artist[20];
        char genre[20];
    };
    and it says that there is a syntax error before '[' token, referencing to the first line ('struct songs [99]')

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You need to define the struct before you can instantiate it. When you say "struct songs {" you're still defining it. You can start declaring instances right after the definition is over.

  5. #5
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    If you want to declare an array of structs, you have a couple of ways of doing it:

    Code:
    struct song //creates a struct called song
    {   
        char track[20];
        char trackname[20]; 
        char artist[20];
        char genre[20];
    } info[99];
    or

    Code:
    struct song //creates a struct called song
    {   
        char track[20];
        char trackname[20]; 
        char artist[20];
        char genre[20];
    } ;
    
    int main(void)
    {
          struct song info[99];
          .....
    
    }
    Either way, you have to declare it before using it, as Zuk suggested

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is a struct definition:
    Code:
    struct whatever {
          [...]
    };
    If you want to use the struct, you need to declare a specific instance:
    Code:
    struct whatever example[99];
    Notice, the name of the instance is not the name of the datatype. This is like how you cannot use "int int", and then try and treat "int" as the name of your variable.

    Technically, you could use "int ints" and "struct song songs", but I'd probably call the struct "track" and the array "songs".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    your original code was nearly ok
    just move
    Code:
    struct song songs[99];
    up
    Kurt

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Thanks all to your replies. I tried all these, but could not sort the problem out -probably because I don't have much programming experience and didn't do it properly.

    However, the code on its own works (without the switch statement), but the switch also works on its own, so it's to do with the way I lay it out when putting it into the switch.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Finally got the code to work!

    For some reason I had made a duplicate of a 'for()' and I'm not sure what else was done to fix it. A friend fixed it and he did it too quickly!

    Thank you all for your time though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Keeping track of static external structure
    By pwilfred in forum C Programming
    Replies: 6
    Last Post: 03-13-2003, 06:23 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM