Here's your code, translated in to C++.
#include <fstream>
#include <iostream>
using namespace std;
void READFile(char* filename)
{
int num;
int color[100]; //make this large enough for any problem set
int value[100];
ifstream inputfile;
inputfile.open( filename );
if ( !inputfile )
{
cout << "file not found, unable to open\n";
return; //added
}
inputfile >> num ;
/*
int color[num]; //oops, not yet legal in C++
int value[num]; //although array declaration with variables
//is supported in some compilers
*/
for (int i = 0; i < num; i++);
{
inputfile >> color[i] >> value[i];
}
}
Note that by declaring the color and value arrays in the function, they only exist in that function. Perhaps they should be declared in main( ) and passed to the function?
I don't think your C input was quite correct, you input to the address of the array, but never incremented to fill other array elements. Should that have been
fscanf(inputfile, "%d","%d", &color[i], &value[i]);
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
Offline 1,895 posts
since Aug 2007