0

This is a really simple program I'm writing here but came up to the problem. well, the program fills the array[2000] from the file and then it should TYPE(cout) the array[i] which isn't equal to the second member of array. I am not sure how to point to the second member of array in program. here is the piece of program:

#include <iostream>
#include <fstream>

using namespace std;
const int N=2000;

int main() {
    int* array;
    array = new int[N];

    for (int i=0; i<N; i++){
        ifstream ifs("reals.txt");
        ifs>>array[i];
    }

    for (int i=0; i<N; i++){
        if(array[i] != array[1])   /// is this right? is array[1] second member? 
            cout<<array[i]<<'\t';
        if((i+1)%13) cout<<endl;
    }
    system("pause");
    return 0;
}

how will it be, if was I wanted to check for the second element from last element of array?

P.S. sorry for my English. if there is anything you can't understand, feel free to comment and I will try to explain. thanks in advance.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182

3 Answers3

5

array[1] is the second member. Your problem is however that you are reopening the file every time in your loop:

for (int i=0; i<N; i++){
    ifstream ifs("reals.txt");
    ifs>>array[i];
}

you need to open the file before the loop:

ifstream ifs("reals.txt");
for (int i=0; i<N; i++){
    ifs>>array[i];
}
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • yeah, you're right. I will fix that. by the way, if I want to make a function the to use in whatever, so I won't know the size of array. how would I point to the second element from the last one? – Geo Drawkcab Jun 15 '12 at 13:02
  • @GeoDrawkcab: The most straightforward way would bew to use std::array (or std::vector for a dynamic array), so the size is always known (myArray.size()) – stefaanv Jun 15 '12 at 13:16
  • @GeoDrawkcab, see [this response](http://stackoverflow.com/questions/11047239/how-can-i-know-legth-of-array-in-c/11047259#11047259) from a question earlier today. – Shahbaz Jun 15 '12 at 13:30
1
for (int i=0; i<N; i++){
    ifstream ifs("reals.txt");
    ifs>>array[i];
}

Should be

ifstream ifs("reals.txt");
for (int i=0; i<N; i++){        
    ifs>>array[i];
}

if(array[i] != array[1]) /// is this right? is array[1] second member?

The answer is yes.

array = new int[N];

don't forget to

delete[] array;

If you want to access the prelast element, there is a drop-in replacement in the standard library for your array:

#include <vector>

int main(){
  std::vector<int> array(1000);
  array[998]= 42;
  int prelast= *(array.end()- 2); // end() is an iterator to one-past the last element
}
nurettin
  • 11,090
  • 5
  • 65
  • 85
  • thanks and if, for example, I want to make a function the to use in whatever, so I won't know the size of array. how would I point to the second element from the last one? is there any kind of command similar to .size() in arrays? – Geo Drawkcab Jun 15 '12 at 13:09
  • in standard c++ we use containers for such things, I updated the answer with an example. – nurettin Jun 15 '12 at 13:32
0

Yes this is correct as arrays are 0 based. For example with an array of 5 elements the last element would be index 4 or array[4];

Carl Winder
  • 938
  • 8
  • 18