array probs

hi I have a big code problem.

we JUST were introduced to arrays this week (maybe 30minutes worth) and now have a program where we need to set up an array of size 20, use a function to import data from a file (he wants the infile IN the function and then output a nunch of thingss like alling diff index numbers etc.

He also wants the outfile OUTside of main.

I can do the manipulation with the data if I even had a clue as to how to set it up to get my data. I have been working all day and have not been able to compile once!

would you look at my code and give me some pointers?

thanks Michele

ps this is the error i am getting:
[code]
binary '>>' : 'class std::basic_ofstream >' does not define this operator or a conversion to a type acceptable to the
predefined operator
[/code]
[code]

#include
#include
#include
#include
#include
using namespace std;



void loadit(int a[],int);
const int SIZE = 20;


ofstream outfile("A:\0F1024_7_1.txt");

int main ()
{





if(!outfile){
cerr << "Cannot open output file" << endl;
return 1;
}


int arr[SIZE]={0};
int x = 0;
loadit(arr,SIZE);
cout<< x << arr[x] << endl;



outfile.close();

return 0;

}


//function def

void loadit(int a[],int x){

ofstream infile("A:\0F1024_7_1.txt");
if(!infile){
cerr << "Cannot open input file" << endl;

}

for (x=0; x < SIZE; x++)
infile >> a[x];
infile >> a[x];
infile.close();


}
[/code]



Comments

  • You have two errors:
    1. In the function main(), "int arr={0};" does not define an array. It should be "int arr[20]={0};" or something like that.
    2. In the function loadit(), change ofstream to fstream and it will work.
  • : thanks those 2 changes helped me to compile, but the function loop is not working, I am getting an output of
    0 and a huge 8 digit number not the numbers i am trying to load into the array from the infile...
    basically i am trying to understand how the array calls a function, if i could get a grip on it it might be alittle less frustrating

    do you think you can help a little more and tell me how i can view or cout each array location 0-19 and it's contents from my infile?
    thanks alot
    Michele





    You have two errors:
    : 1. In the function main(), "int arr={0};" does not define an array. It should be "int arr[20]={0};" or something like that.
    : 2. In the function loadit(), change ofstream to fstream and it will work.
    :


  • : ps I've tried putting the output from the function...and it will list all my locations but each index is listing as zero!!??
    :
    :
    :
    : You have two errors:
    : : 1. In the function main(), "int arr={0};" does not define an array. It should be "int arr[20]={0};" or something like that.
    : : 2. In the function loadit(), change ofstream to fstream and it will work.
    : :
    :
    :

  • To display each of the cells, you need a loop like this one in your main()
    [code]
    int arr[20]={0};
    loadit(arr,SIZE);
    for(int x = 0; x < 20; x++)
    cout<< x << arr[x] << endl;
    [/code]

  • :thanks Stober _ I am an idiot...I was using my outfile instead of my IN...
    I'm SUCH an IdioT!
    thanks :-D










    To display each of the cells, you need a loop like this one in your main()
    : [code]
    : int arr[20]={0};
    : loadit(arr,SIZE);
    : for(int x = 0; x < 20; x++)
    : cout<< x << arr[x] << endl;
    : [/code]
    :
    :

  • The reason your program does not seem to work is because outfile is destroying the file's contents. Rearrange your program a little, re-create the text file and it will work ok.
    [code]
    void loadit(int a[],int);
    const int SIZE = 20;
    int arr[20];


    int main ()
    {
    loadit(arr,SIZE);
    ofstream outfile("A:\0F1024_7_1.txt");
    if(!outfile){
    cerr << "Cannot open output file" << endl;
    return 1;
    }
    // rewrite the array here or do something else
    // with outfile
    outfile.close();

    return 0;

    }

    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories