Thread: Help needed w/ line of code..Thanks!

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Help needed w/ line of code..Thanks!

    Hi..
    I am trying to read from a string into various structure fields..what am I doing wrong..
    pStuPtr is a pointer to the array of structures 'table'.

    sscanf(string,"%s%s%d%f%f%f%f%f", pStuPtr->names, pStuPtr->lastNames, &pStuPtr->id, pStuPtr->scores[0],
    pStuPtr->scores[1], pStuPtr->scores[2], pStuPtr->scores[3], pStuPtr->scores[4]);

    thanks
    A

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Facinating. And without seeing your structure, I'm supposed to assume you're doing this all correctly? If so, why would you be posting?

    Show me the structure and the variable declaration.

    If it is a pointer to "a table", which I assume is an array, then you are scanning incorrectly.

    struct mystruct mytable[5];

    sscanf( string, "%d", &mytable[x].myvariable[y] );


    Quzah.
    Last edited by quzah; 05-30-2002 at 01:39 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    this is my entire code: Hope it helps..how do you do the code tag thing?? sorry..I don't know yet
    A
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    #define NUM_STU 15


    /* Prototype Declarations */

    typedef struct{ char names[20];
    char lastNames[20];
    int id;
    float scores[5];
    } STRUCT;

    typedef enum{names,
    id,
    quiz1,
    quiz2,
    quiz3,
    quiz4,
    exam} STU;


    int fillStructure(STRUCT table[]);

    void printStruct(STRUCT table[]);

    int main(void)
    {


    STRUCT table[50];

    int check;




    check = fillStructure(table);
    while(!check)
    {
    printf("Wazzup\n");
    printStruct(table);
    }
    return 0;
    }

    int fillStructure(STRUCT table[])
    {
    /* Local Definitions */

    FILE *fpnum;
    char string[100];

    /* Statements */
    STRUCT *pStuPtr;

    if (fpnum = fopen ("lab5.txt", "r")) // Opens lab5.txt
    for(pStuPtr = table; pStuPtr < table + NUM_STU; pStuPtr++)
    {
    fgets(string, 100, fpnum);
    puts(string);
    }
    else
    {
    printf("\aError: Was Unable to Open File.\n");
    return 1;
    }
    return 0;
    }

    /***printStruct****/


    void printStruct(STRUCT table[])
    {
    STRUCT *pStuPtr;

    for(pStuPtr = table; pStuPtr < table + NUM_STU; pStuPtr++)
    {
    printf("%s%s%d%f%f%f%f%f", pStuPtr->names, pStuPtr->lastNames, pStuPtr->id, pStuPtr->scores[0],
    pStuPtr->scores[1], pStuPtr->scores[2], pStuPtr->scores[3], pStuPtr->scores[4]);
    }

    return;
    }

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > how do you do the code tag thing??
    Read the FAQ.
    The world is waiting. I must leave you now.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > #define NUM_STU 15

    > STRUCT table[50];

    Why does your table have 50 entries when you only need 15?
    By the way, your choice of typedef names is horrible, IMO.

    STRUCT table[NUM_STU];


    void printStruct(STRUCT table[])
    {
    STRUCT *pStuPtr;

    for(pStuPtr = table; pStuPtr < table + NUM_STU; pStuPtr++)
    {
    printf("%s%s%d%f%f%f%f%f", pStuPtr->names, pStuPtr->lastNames, pStuPtr->id, pStuPtr->scores[0],
    pStuPtr->scores[1], pStuPtr->scores[2], pStuPtr->scores[3], pStuPtr->scores[4]);
    }

    return;
    }
    Is there some particular reason you need to use a pointer here? A requirement for the assignment or something? There really is no need.

    Code:
    void printStruct(STRUCT table[]) 
    { 
        int x;
    
        for( x = 0; x < NUM_STU; x++ )
            printf("%s %s %d %f %f %f %f %f",
                table[x].names, table[x].lastNames, table[x].id,
                table[x].scores[1], table[x].scores[2], table[x].scores[3],
                table[x].scores[4], table[x].scores[5]
            );
    }
    You can do the same thing for scanning. Either way will work. You just don't have to use a pointer there.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    the problem does not lie in the printing but the scanning of informatiom from the file into the structure
    I am using a buffer string to collect info
    and from there send it out to the various fields..I don't see why is it not working..
    Thanks though
    A

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by aspand
    the problem does not lie in the printing but the scanning of informatiom from the file into the structure
    I am using a buffer string to collect info
    and from there send it out to the various fields..I don't see why is it not working..
    Thanks though
    A
    You don't see why it isn't working? I honestly don't see how you think it can be working. Where in your code are you filling the structures? Nowhere!

    You have no code that is scanning into a structure. That's why it doesn't work. No place in your code example are you scanning into a structure.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Thanks Quzah..that works!! you're a legend..(sometimes obnoxious in tone..but apart from that great)
    A

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 10:21 PM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM