Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

last post re: sort func. is fixed; nedd help with read/write funcs.

Status
Not open for further replies.

tinycities

Programmer
Oct 31, 2000
6
US
I posted last night regarding my sort function, however, it turns out that wasn't the problem at all, it was my write function. I have since fixed that problem within my write function. The problem I'm dealing with now, is that my read func. seems not to be getting the final number from my input file. I've pasted both my read and write funcs. below. (in case it's actually a write error). The output is good, except that it excludes that final number.

void read (istream& in_str, int arr[], int size, int& num_used)
{

for(int index = 0; index < size-1 && in_str >> arr[index]; index++)
{
num_used = index;
}//end for
}//end read

void write (ostream& out_str, int arr[], int size, int& num_used)
{
for (int index = 0; index < size && index < num_used; index++)
{
out_str << arr[index];
out_str << &quot; &quot;;
}
out_str << endl;
}
 
I suggest that although your &quot;for&quot; statement looks very sophisticated, it may be unduely comlicated and could be causing the problem. C++ does not guarentee the order in which &quot;AND&quot; operations are performed. Break the &quot;for&quot; down to just the Index < limit, and add a &quot;break&quot; statement inside the loop at the appropreate time to exit the &quot;for&quot; loop.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top