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 426,465 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,240 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: 1827 | Replies: 2
Reply
Join Date: Jul 2004
Location: Md
Posts: 17
Reputation: nufanvandal is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
nufanvandal nufanvandal is offline Offline
Newbie Poster

Counter issues

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

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 || ageList[i] != 0; i++)
   {
      
      scanf("%d",&ageList[i]);
      counter += 1;
   }

   /*Prints the integers*/
   for(i = 0; i <= MAX && ageList[i] != 0;i++)
   {
    	 printf("%d\n",ageList[i]);
   }
      return(counter);
}    
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,631
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Counter issues

  #2  
Jul 22nd, 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.
#include <stdio.h>
size_t FillAgeList(int ageList[], size_t size)
{
size_t i, counter = 0;
/* Reads the data from .dat file */ 
for ( i = 0; i < size; ++i )
{
	 if ( scanf("%d", &ageList[i]) != 1 || ageList[i] == 0 )
	 {
		 break;
	 }
	 ++counter;
}
/* Prints the integers */
for ( i = 0; i < counter; ++i )
{
	 printf("%d\n", ageList[i]);
}
return counter;
}	
int main(void)
{
int array[10];
size_t size = FillAgeList(array, sizeof array / sizeof *array);
printf("size = %d\n", (int)size);
return 0;
} 
[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;
} 
Reply With Quote  
Join Date: Jul 2004
Location: Md
Posts: 17
Reputation: nufanvandal is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
nufanvandal nufanvandal is offline Offline
Newbie Poster

Re: Counter issues

  #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  
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 3:48 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC