| | |
Counter issues
![]() |
•
•
Join Date: Jul 2004
Posts: 17
Reputation:
Solved Threads: 0
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.
The whole function:
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:
C Syntax (Toggle Plain Text)
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); }
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.
[edit]Oh, wait. I think this is it. 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.
C Syntax (Toggle Plain Text)
#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; }
for(i = 0; i <= MAX || ageList[i] != 0; i++)
#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
![]() |
Similar Threads
- ASP .NET hit counter? (ASP.NET)
- PHP Hit Counter ISSUES. (PHP)
Other Threads in the C Forum
- Previous Thread: AnsiString to Const Char*
- Next Thread: piglatin
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






