0

Hi,

I'm quite bad in c++ and i have this weird (for me), problem that i can't work around.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
.....
int main(){
...
                int *array1=NULL;
		int *buffer=NULL;
		size=....;//a certain size comes from function
		array1= new int[size];// it looks like both array1 and buffer have only 1 element in them. 
		buffer= new int[size];//
...
                file_read(array1,false); //The crazy thing is, when i give static size to the arrays, i can see the array elements but when i send them through this function, the arrays appear on the other side as having 1 element.

This is the part of function where something is wrong.

int file_read(int a[], bool first)
....
		while(!myfile.eof())
   		{
  	 		myfile>> a[i];//the first and only element gets a value, other than that, i keeps increasing, the eof is never reached.
      			i++;
		}
...
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

The loop in the read function is incorrect. Using eof() like that will cause the last line to be read twice. Here is the standard way to code that loop

while( myfile >> a[i] )
   ++i;

>>The crazy thing is, when i give static size to the arrays, i can see the array elements but when i send them through this function,

Might be your debugger. vc++ 2008 express debugger does the same thing. But don't worry, its really ok.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.