Hi, I'm trying to make a program that reads a text file that contains about 4 rows of numbers and outputs the average for each row. I need help with these two errors. It is not complete but I need help on these so I can continue. Any help is appreciated. Thanks in advanced.

Error 1 error C2109: subscript requires array or pointer type line 41

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion) line 52

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_CODE_SIZE = 200;

bool die(const string & msg);

int main(){
	ifstream incode;
	ofstream outcode;

	char inputFile[30];
	char outputFile[30];

	cout << "Enter your input file name: ";
	cin >> inputFile;
	cout << endl;

	
	incode.open(inputFile);
	if (!incode){ bool die("File opening error");
	
	cout << "Enter the name for your output file: ";
	cin >> outputFile;
	cout << endl;

	outcode.open (outputFile);
	unsigned sum = 0;
	unsigned col = 0;
	unsigned row = 0;
	unsigned matrix = 0;
	unsigned NUMBER_OF_ROWS;
	unsigned NUMBER_OF_COLUMNS;
	for (row=0;row < NUMBER_OF_ROWS; row++)
	{
		sum=0;
		for (col = 0; col < NUMBER_OF_COLUMNS; col++)
			sum = sum + matrix[row][col];
			cout << "AVERAGE OF ROW IS " << row + 1 / NUMBER_OF_COLUMNS << endl;
	
	incode.close();
	outcode.close();

	return 0;
	}
	}  }

bool die(const string & msg) {
	cout << "FATAL ERROR " << msg <<endl;
	exit(EXIT_FAILURE); 

}

Hi, I'm trying to make a program that reads a text file that contains about 4 rows of numbers and outputs the average for each row. I need help with these two errors. It is not complete but I need help on these so I can continue. Any help is appreciated. Thanks in advanced.

Error 1 error C2109: subscript requires array or pointer type line 41

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion) line 52

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_CODE_SIZE = 200;

bool die(const string & msg);

int main(){
	ifstream incode;
	ofstream outcode;

	char inputFile[30];
	char outputFile[30];

	cout << "Enter your input file name: ";
	cin >> inputFile;
	cout << endl;

	
	incode.open(inputFile);
	if (!incode){ bool die("File opening error");
	
	cout << "Enter the name for your output file: ";
	cin >> outputFile;
	cout << endl;

	outcode.open (outputFile);
	unsigned sum = 0;
	unsigned col = 0;
	unsigned row = 0;
	unsigned matrix = 0;
	unsigned NUMBER_OF_ROWS;
	unsigned NUMBER_OF_COLUMNS;
	for (row=0;row < NUMBER_OF_ROWS; row++)
	{
		sum=0;
		for (col = 0; col < NUMBER_OF_COLUMNS; col++)
			sum = sum + matrix[row][col];
			cout << "AVERAGE OF ROW IS " << row + 1 / NUMBER_OF_COLUMNS << endl;
	
	incode.close();
	outcode.close();

	return 0;
	}
	}  }

bool die(const string & msg) {
	cout << "FATAL ERROR " << msg <<endl;
	exit(EXIT_FAILURE); 

}

Hi Book,

You have declared some variable and using without initialization. I think your code is incomplete. Post the full code. The following code snippet has error,

bool die("File opening error"); //Is this a function call or function declaration??
unsigned NUMBER_OF_ROWS; //declared without defined
       unsigned NUMBER_OF_COLUMNS; //declared without defined
	for (row=0;row < NUMBER_OF_ROWS; row++) // Using without defined
	{
		sum=0; // No declaration
		for (col = 0; col < NUMBER_OF_COLUMNS; col++) // Using without defined
			sum = sum + matrix[row][col];
			cout << "AVERAGE OF ROW IS " << row + 1 / NUMBER_OF_COLUMNS << endl;
 
	incode.close();
	outcode.close();
 
	return 0;
	}
exit(EXIT_FAILURE); // Using without defined

Where are you reading numbers from file?? You mentioned that input file contains 4-rows of number but there is no such statement for reading from file...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.