User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,530 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,812 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 293 | Replies: 1
Reply
Join Date: Oct 2007
Posts: 4
Reputation: ITapprentice is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ITapprentice ITapprentice is offline Offline
Newbie Poster

small error with structs

  #1  
Oct 8th, 2007
I'm back and I 've changed my code around a bit. I almost have it compiling except for one error that says there is a syntax error before inFile my struct that will hold my arrays. It's in the second for loop. I think that I have my struct declared correctly and I think that it will actually hold each line from the text file as an array into my struct. Here's my code

//Struct representing the items in the file
struct inFile 
{
    char itemNumber[32];
    int timeToFinish;
    int elapsedMinutes;
};

typedef struct inFile inFile;

int main ()
{
    char fileName[100];
    int itemAmount = 0;
    char itemNumber[10];
    int timeToFinish = 0;
    int elapsedMinutes = 0;
    int i = 0;
    int readCounter = 0;
    int finishTime[20];//holds the finish items
    int counter = 0;
    
    // Prompt the user for a filename.
    printf ("Enter a filename to load: ");
    scanf ("%s" , &fileName);
    
    FILE *fn;
    fn = fopen(fileName, "r");
    
    // Check to make sure that we opened the file successfully.
    if (fn != NULL) 
    {
        fscanf (fn, "%d", &itemAmount);//Scan first line into variable
                  
        int finishTime[15];          
        // Initialize it to NULLs
        /*
        for (i=0; i<15; i++) 
        {
            finishTime[i] = NULL;
        }
        */
        
        //Loading each item from the file into our array.
        //while (i <= itemAmount)
        for (i=0; i<itemAmount; i++)
        {
                fscanf(fn, "%s, %d, %d,", inFile.itemNumber[i], inFile.timeToFinish[i], inFile.elapsedMinutes[i]);      //HERE I HAVE AN ERROR BEFORE INFILE                        

finishTime[counter] = timeToFinish;
counter++;
                               
        }
        
        //Closes the file
        printf ("\nFile successfully loaded!\n");
        fclose(fn);
                
        // Print out the array contents.
        for (readCounter = 0; readCounter < 15; readCounter++) 
        {
            if (finishTime[readCounter] != 0) 
            {
                printf("item%d %d %d\n", readCounter, finishTime[readCounter]);
            }
        }
    }

    else 
    {
        printf("Error: The file you specified could not be found!");
    }
    
    system ("PAUSE");
    return 0;
}
So frustrated because I'm almost there! Just one little error and a couple of my friends couldn't figure it out either.
Last edited by Ancient Dragon : Oct 8th, 2007 at 10:39 pm. Reason: correct code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: small error with structs

  #2  
Oct 8th, 2007
struct inFile 
{
    char itemNumber[32];
    int timeToFinish;
    int elapsedMinutes;
};

typedef struct inFile inFile;
You create a structure data type and even typedef but you do not define any variable of type structure for your main to use.

 fscanf(fn, "%s, %d, %d,", inFile.itemNumber[i], inFile.timeToFinish[i], inFile.elapsedMinutes[i]);
You are forgetting to use the & with the elements that are of type int and char alone.

    char itemNumber[10];
    int timeToFinish = 0;
    int elapsedMinutes = 0;
Those variables have the same identifier names that the elements in the structure declaration.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC