comp115 assignment 3 (1 Viewer)

kunny funt

Large Member
Joined
Oct 29, 2004
Messages
153
Gender
Male
HSC
2004
ok this is the weirdest problem. I am using a getline function to count the number of students in a certain file:

int n;
while(!inFile.eof())
{
getline(inFile, myString);
n++;
}

which is simple enough right, ill get a value for n which i can use throughout the whole program, well apparently not.

HOwever, if instead of using this function, i just set the value of n to be the number(which i know for this certain file) like instead of the thing above, i just write:

int n=40;

then the program works

W T F......... srsly, its like exactly the same thing yet only one way works and i cant use that way for all input files.

can anyone tell me why????????????????
 

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
Try putting a line inside the loop that prints out what the number is each time the loop executes, it may show you were the problem is occurring ;)
 

kunny funt

Large Member
Joined
Oct 29, 2004
Messages
153
Gender
Male
HSC
2004
yes ... doing it in array ........ acmilan, already done, it showed that the loop works out the correct numbers every time and when i put this loop in /*..*/ and store the values manually ie : int n = watever then the program works perfectly
 

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
if you are doing it in arrray
it is a rule that you HAVE to define the array size before you start using it
 

Mini

relax
Joined
Nov 13, 2003
Messages
83
Location
here... duh
Gender
Female
HSC
2003
kunny funt said:
ok this is the weirdest problem. I am using a getline function to count the number of students in a certain file:

int n;
while(!inFile.eof())
{
getline(inFile, myString);
n++;
}

...

can anyone tell me why????????????????
u didn't initialise n

this should work:

int n=0;
while(!inFile.eof())
... etc etc
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top