943,708 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2538
  • C RSS
Jul 22nd, 2004
0

Counter issues

Expand Post »
Hey everyone.

im having issues with my for loop:
basically it reads in from a file using unix redirection and stores the integers into an array, that all works fine, but i need to return the number of integers that were read in, and logically i thought this would work, but i just get some crazy number. thanks for any help.

 /*Reads the data from .dat file*/  
   for(i = 0; i <= MAX || ageList[i] != 0; i++)
   {
      
      scanf("%d",&ageList[i]);
      counter += 1;
   }

The whole function:

  1.  
  2. int FillAgeList(int ageList[])
  3. {
  4.  
  5. int i;
  6. int counter;
  7.  
  8. /*Initializes the array*/
  9. for(i = 0; i <= MAX; i++)
  10. {
  11. ageList[i] = -1;
  12. }
  13.  
  14. counter = 0;
  15. /*Reads the data from .dat file*/
  16. for(i = 0; i <= MAX || ageList[i] != 0; i++)
  17. {
  18.  
  19. scanf("%d",&ageList[i]);
  20. counter += 1;
  21. }
  22.  
  23. /*Prints the integers*/
  24. for(i = 0; i <= MAX && ageList[i] != 0;i++)
  25. {
  26. printf("%d\n",ageList[i]);
  27. }
  28. return(counter);
  29. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004
Jul 23rd, 2004
0

Re: Counter issues

I'd be careful with the <=, and I wish I'd spent more time looking into the 'Why doesn't this work?', but you may want to try something like this.
  1. #include <stdio.h>
  2. size_t FillAgeList(int ageList[], size_t size)
  3. {
  4. size_t i, counter = 0;
  5. /* Reads the data from .dat file */
  6. for ( i = 0; i < size; ++i )
  7. {
  8. if ( scanf("%d", &ageList[i]) != 1 || ageList[i] == 0 )
  9. {
  10. break;
  11. }
  12. ++counter;
  13. }
  14. /* Prints the integers */
  15. for ( i = 0; i < counter; ++i )
  16. {
  17. printf("%d\n", ageList[i]);
  18. }
  19. return counter;
  20. }
  21. int main(void)
  22. {
  23. int array[10];
  24. size_t size = FillAgeList(array, sizeof array / sizeof *array);
  25. printf("size = %d\n", (int)size);
  26. return 0;
  27. }
[edit]Oh, wait. I think this is it.
 for(i = 0; i <= MAX || ageList[i] != 0; i++)
You increment i past any known values and then check to see whether this is a zero, when you've already initialized it to -1 (if you havent' wandered off the end of the array). So this might also cure things.
#include <stdio.h>
#define MAX 10
int FillAgeList(int ageList[])
{
 
int i; 
int counter;
 
/*Initializes the array*/
for(i = 0; i <= MAX; i++)
{
	 ageList[i] = -1;
}
counter = 0; 
/*Reads the data from .dat file*/ 
for(i = 0; i <= MAX; i++)
{
 
	 scanf("%d",&ageList[i]);
	 if(ageList[i] == 0)
	 {
		 break;
	 }
	 counter += 1;
}
/*Prints the integers*/
for(i = 0; i <= MAX && ageList[i] != 0;i++)
{
	 printf("%d\n",ageList[i]);
}
	 return(counter);
}	
int main(void)
{
int array [ MAX + 1 ], size = FillAgeList(array);
printf("size = %d\n", size);
return 0;
}
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 23rd, 2004
0

Re: Counter issues

Hey, thanks a ton, that worked quite nicely...at least i was on the right track i suppose.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nufanvandal is offline Offline
18 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: AnsiString to Const Char*
Next Thread in C Forum Timeline: piglatin





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC