small error with structs

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 4
Reputation: ITapprentice is an unknown quantity at this point 
Solved Threads: 0
ITapprentice ITapprentice is offline Offline
Newbie Poster

small error with structs

 
0
  #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

  1. //Struct representing the items in the file
  2. struct inFile
  3. {
  4. char itemNumber[32];
  5. int timeToFinish;
  6. int elapsedMinutes;
  7. };
  8.  
  9. typedef struct inFile inFile;
  10.  
  11. int main ()
  12. {
  13. char fileName[100];
  14. int itemAmount = 0;
  15. char itemNumber[10];
  16. int timeToFinish = 0;
  17. int elapsedMinutes = 0;
  18. int i = 0;
  19. int readCounter = 0;
  20. int finishTime[20];//holds the finish items
  21. int counter = 0;
  22.  
  23. // Prompt the user for a filename.
  24. printf ("Enter a filename to load: ");
  25. scanf ("%s" , &fileName);
  26.  
  27. FILE *fn;
  28. fn = fopen(fileName, "r");
  29.  
  30. // Check to make sure that we opened the file successfully.
  31. if (fn != NULL)
  32. {
  33. fscanf (fn, "%d", &itemAmount);//Scan first line into variable
  34.  
  35. int finishTime[15];
  36. // Initialize it to NULLs
  37. /*
  38.   for (i=0; i<15; i++)
  39.   {
  40.   finishTime[i] = NULL;
  41.   }
  42.   */
  43.  
  44. //Loading each item from the file into our array.
  45. //while (i <= itemAmount)
  46. for (i=0; i<itemAmount; i++)
  47. {
  48. fscanf(fn, "%s, %d, %d,", inFile.itemNumber[i], inFile.timeToFinish[i], inFile.elapsedMinutes[i]); //HERE I HAVE AN ERROR BEFORE INFILE
  49.  
  50. finishTime[counter] = timeToFinish;
  51. counter++;
  52.  
  53. }
  54.  
  55. //Closes the file
  56. printf ("\nFile successfully loaded!\n");
  57. fclose(fn);
  58.  
  59. // Print out the array contents.
  60. for (readCounter = 0; readCounter < 15; readCounter++)
  61. {
  62. if (finishTime[readCounter] != 0)
  63. {
  64. printf("item%d %d %d\n", readCounter, finishTime[readCounter]);
  65. }
  66. }
  67. }
  68.  
  69. else
  70. {
  71. printf("Error: The file you specified could not be found!");
  72. }
  73.  
  74. system ("PAUSE");
  75. return 0;
  76. }
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,044
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: small error with structs

 
0
  #2
Oct 8th, 2007
  1. struct inFile
  2. {
  3. char itemNumber[32];
  4. int timeToFinish;
  5. int elapsedMinutes;
  6. };
  7.  
  8. 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.

  1. 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.

  1. char itemNumber[10];
  2. int timeToFinish = 0;
  3. int elapsedMinutes = 0;
Those variables have the same identifier names that the elements in the structure declaration.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC