Here we go again. As you can see, I have gotten much further. There are some elements however that I am unsure how to apply (i.e. bool tooMany). I haven't the slightest how to apply that. That is one snag that I have. Another, and the main one, is this:

The following code does work. It calls a file called "studentData.txt". Said file contains the ID#s and Scores on their own lines :
(id) 101
(score) 100
102
95
103
90
...
...
121
0

Now, if I comment that out and just have it read from the arrays that I have hardcoded it works great. I can't quite figure out how to compute the .txt items into the individual arrays to make it use those as opposed to the hardcoded arrays. One of my main issues with reading from a .txt file, is that the only way I know how is using the getline feature. Is there anything better?

Currently I have the code calling the .txt file.

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

using namespace std;
void printTable(int score[], int id[], int count);
void printGrade(int oneScore, float average);

void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
	const int MAX_SIZE = 21;
	rss.open("studentData.txt");
	string line;
	id[MAX_SIZE];
	int score[MAX_SIZE];
	count = 0;
	int oneScore = 0;
	float average = 0;
	string grade;

	for(count = 0; count < MAX_SIZE; count++)
	{
		getline(rss,line);
		cout << line;
		getline(rss,line);
		cout << "  " << line;
		cout << "  " << grade << endl;
	}

//	printTable(score, id, count);

}
float computeAverage(int scores[], int count[])
{
	const int MAX_SIZE = 21;

	return 0;
}
void printTable(int score[], int id[], int count)
{
	void printGrade(int oneScore, float average);
	const int MAX_SIZE = 21;
	int oneScore = 0;
	float average = 0;
	string grade;
	id[MAX_SIZE];
	score[MAX_SIZE];

	cout << left << setw(9) << "ID#s" << setw(9) << "Scores" << setw(9) << "Grades" << endl << endl;
	//for(count = 0; count < MAX_SIZE; count++)
	//{
		printGrade(oneScore,average);
	//}

}
void printGrade(int oneScore, float average)
{
	const int MAX_SIZE = 21;

	int id[MAX_SIZE] = {101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121};
	int scores[MAX_SIZE] = {100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5,0};
	oneScore = 0;
	average = 0;
	string grade;

	int sum = 0;
	for(int i = 0; i < MAX_SIZE; i++)
	sum += scores[i];
	average = sum / MAX_SIZE;

	for(int i = 0; i < MAX_SIZE; i++)
	{
		if(scores[i] > average + 10)
		{
			grade = "outstanding";
		}
		else if(scores[i] < average - 10)
		{
			grade = "unsatisfactory";
		}
		else
		{
			grade = "satisfactory";
		}

//		cout << left << setw(9) << id[i] << setw(9) << scores[i] << setw(9) << grade << endl;
	}
	
}
int main()
{
	ifstream rss;
	string line;
	const int MAX_SIZE = 21;
	int scores[MAX_SIZE];
	int id[MAX_SIZE];
	int count;
	bool tooMany;

	readStudentData(rss, scores, id, count, tooMany);

	

	return 0;
}

Recommended Answers

All 2 Replies

DO NOT declare new arrays inside your functions!

You've already declared them and provided storage for them in main here:

const int MAX_SIZE = 21;
	rss.open("studentData.txt");
	string line;
	id[MAX_SIZE];
	int score[MAX_SIZE];

Don't do it anywhere else, like here:

void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
	const int MAX_SIZE = 21;
	rss.open("studentData.txt");
	string line;
	id[MAX_SIZE];
	int score[MAX_SIZE];
	count = 0;
	int oneScore = 0;
	float average = 0;
	string grade;

	for(count = 0; count < MAX_SIZE; count++)
	{
		getline(rss,line);
		cout << line;
		getline(rss,line);
		cout << "  " << line;
		cout << "  " << grade << endl;
	}

//	printTable(score, id, count);

}

Red is where you're redeclaring your array. Don't do it. Green are the arrays the function has been passed. Delete the red lines. Anything having to do with the arrays needs to involve the array names given in green above.

Ditto for any declaration of arrays or MAX_SIZE in any other function. I imagine that, assuming MAX_SIZE needs to be visible inside the functions, you need to make it a global variable. However, I'm not sure MAX_SIZE needs to be seen in any other function. But I don't know. The tooMany variable and the function specifications puzzle me a bit. Keep in mind that this is a new thread, so most people, unlike me, haven't seen your last threads and thus don't know about it or the function specifications in it.

Google "variable scoping" and "global versus local variable" to get a better feel of why I am suggesting you should NOT declare these arrays inside of your functions. Declare them once in main, then pass them everywhere to your functions. When you do that, you don't need to redeclare them.

You should get clarification on what the boolean tooMany variable stands for. Note you have MAX_SIZE set equal to 21. Do you know for sure you'll have EXACTLY 21? Could you not have less? If so, you have a for-loop that tries to read in 21 lines. What if there are only 14 lines? Since count is passed by reference, I am guessing that you don't know how many lines there will be.

Thanks. That helped trim down my code a bit and I managed to get a few other things done with it. Appreciate the help.

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.