Counter issues

Reply

Join Date: Jul 2004
Posts: 17
Reputation: nufanvandal is an unknown quantity at this point 
Solved Threads: 0
nufanvandal nufanvandal is offline Offline
Newbie Poster

Counter issues

 
0
  #1
Jul 22nd, 2004
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Counter issues

 
0
  #2
Jul 23rd, 2004
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;
}
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 17
Reputation: nufanvandal is an unknown quantity at this point 
Solved Threads: 0
nufanvandal nufanvandal is offline Offline
Newbie Poster

Re: Counter issues

 
0
  #3
Jul 23rd, 2004
Hey, thanks a ton, that worked quite nicely...at least i was on the right track i suppose.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC