9 Replies - 1877 Views - Last Post: 30 March 2010 - 07:32 PM Rate Topic: -----

#1 astroshark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Trying to read ids and scores from text file into an array

Posted 29 March 2010 - 05:00 PM

Hello I am trying to get student ids and student scores from a text file and read them into arrays in the "readStudentData" function, and if the file has more than 21 lines then the program is just supposed to read 21 lines and no more, but I am not that great at using arrays or doing for loops and i've been trying for hours to get it to work but no luck just getting errors for using bool wrong and using the arrays wrong. Any help would be appreciated. I've also attached the text file that we are supposed to use to test our program.
Attached File  20StuA.txt (222bytes)
Number of downloads: 74
Attached File  21StuA.txt (234bytes)
Number of downloads: 108
Attached File  22StuA.txt (246bytes)
Number of downloads: 80

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

void readStudentData (ifstream &rss, // IN: data file
		      int scores[], // OUT: the scores
		      int id[], // OUT: the IDs
		      int &count, // OUT: Number of students read
		      bool &tooMany); // OUT: a flag to indicate that more
				      // than MAX_SIZE scores items are in
				      // input file.

// computes average of count student scores
float computeAverage (int scores[], int count);

// Displays a table showing each student's ID, score and grade
// on a seperate line
// Uses: calculateGrade
void printTable (int scores[], int id[], int count);

// Prints student grade after comparing oneScore to average
void calculateGrade (int oneScore, float average);

// Opens up the input file
void openInputFile (ifstream &rss);

int main()
{
	const int MAX_SIZE = 21;
	bool tooMany;
	ifstream rss;
	int count, scores[], id[];
	openInputFile (rss);
	readStudentData (rss, scores, id, count, tooMany);
	printTable (scores, id, count);
	return 0;
}
void openInputFile (ifstream &rss)
{
	string rss2;
	cout << "Please enter in the name of the file that you want to open:" << endl;
	cin >> rss2;

	rss.open (rss2.c_str());

	if (rss.fail())
	{
	cerr <<  "ERROR: cannot open " << rss2
		 <<  " for input." << endl;
	exit(1);
	}
}

void readStudentData (ifstream &rss,
		      int scores[],
		      int id[],
		      int &count,
		      bool &tooMany)
{
	int line = 0;
	if (tooMany < 21)
	{
		tooMany = false;
	}
	else
	{
		tooMany = true;
	}
	while(tooMany != true)
	{
		for (count = 0; count < line; count++)
		{
			cin >> id;
			id[line] = id;
			cin >> scores;
			scores[line] = scores;
			getline(rss, line);
			line++;
		}
	}
	float average = computeAverage (scores, count);
}

float computeAverage (int scores[], int count)
{
	float sum = 0, average;

	for (int index = 0; index < count; index++)
	{
	  sum += scores[index];
	}
	average = sum / count;
	return average;
}


This post has been edited by astroshark: 29 March 2010 - 05:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to read ids and scores from text file into an array

#2 Tethik   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 63
  • Joined: 14-March 10

Re: Trying to read ids and scores from text file into an array

Posted 29 March 2010 - 05:43 PM

void readStudentData (ifstream &rss,
                      int scores[],
                      int id[],
                      int &count)
{
        if (count > 20) count = 20;

        

        for (int line = 0; line < count; line++)
        {
		string unformated = "";

               /* cin >> id[line];
                cin >> scores[line]; */

                rss.getline(unformated, line);

		int i = 0;
		string formated = "";
		for(; i < unformated.length(); ++i)
		{
			if(unformated[i] == '\t') break;

			formated += unformated[i];
		}
				
		id[line] = atoi(formated.c_str());

		for(formated = ""; i < unformated.length(); ++i)
		{
			formated += unformated[i];
		}

		scores[line] = atoi(formatted.c_str());
				
        }

    
        float average = computeAverage (scores, count);
}



Havent tried compiling that, but I hope you get the general idea. Go through every line and split it into 2 strings where there is a tab (at least I think its a tab), edit arrays with these new strings (atoi converts to int).

EDIT: rss.getline doesnt work like I thought.

This post has been edited by Tethik: 29 March 2010 - 05:49 PM

Was This Post Helpful? 1
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Trying to read ids and scores from text file into an array

Posted 29 March 2010 - 05:55 PM

I think your overthinking the problem..

take a look at this function
#define MAX 21
void readStudentData(int *id, int *score, ifstream &file)
{
	int i = 0;
	while(file>>id[i]>>score[i]) //no need to parse at the tab, just read it in
	{
		i++;
		if(i>=MAX) break;	
	}
	
}



If you pass pointer rather than array you will avoid making a copy of the array in the function an you can actual populate the array that is passed in rather then having the copy go out of scope.. if that make sense

call it like this

int main ()
{
	ifstream file("file.txt");
	int id[MAX], score[MAX];
	readStudentData(id,score,file); //function call
	


This post has been edited by ImaSexy: 29 March 2010 - 05:58 PM

Was This Post Helpful? 1
  • +
  • -

#4 astroshark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 06:49 PM

Ok here is the code I got from some of the help received but when i try to compile it gives me
error C2440: '=': cannot convert from 'const char [15]' to 'char'
error C2440: '=': cannot convert from 'const char [12]' to 'char'
error C2440: '=': cannot convert from 'const char [13]' to 'char'
for the lines
'
gradeString[index] = "Unsatisfactory";
gradeString[index] = "Outstanding";
else gradeString[index] = "Satisfactory";
'
in the calculateGrade function

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

void readStudentData (int id[], int score[], ifstream &rss,
								int &count, bool &tooMany, string gradeString);

float computeAverage (int score[], int count);

void printTable (int id[], int score[], int count, string gradeString);

void calculateGrade (int score[], int id[], string gradeString, int count,
                         float average);

void openInputFile (ifstream & rss);

int main()
{
	const int MAX_SIZE = 21;
	ifstream rss;
	int id[MAX_SIZE], score[MAX_SIZE];
	string gradeString;
	int count;
	bool tooMany;

	openInputFile (rss);
	readStudentData (id, score, rss, count, tooMany, gradeString);
	if (tooMany)
	{
	  cout << "More than 21 items so not all could be processed"  << endl;
	}
	printTable (id, score, count, gradeString);

	return 0;
}

void readStudentData (int id[], int score[], ifstream &rss,
								int &count, bool &tooMany, string gradeString)
{
	/*
  data[0].id = 12345;
  data[0].score = 90;
  data[1].id = 23456;
  data[1].score = 40;
  data[2].id = 99999;
  data[2].score = 65;

  count = 3;*/
	int i = 0;
    while(rss >> id[i] >> score[i])
    {
		i++;
		if(i>=21) break;       
	}
	count = i;
  tooMany = false;

  float average = computeAverage (score, count);
  calculateGrade (score, id, gradeString, count, average);
}

float computeAverage (int score[], int count)
{
  float sum = 0, average;

  for (int index = 0; index < count; index++)
  {
    sum += score[index];
  }
  average = sum / count;
  return average;
}

void printTable (int id[], int score[], int count, string gradeString)
{
  for (int index = 0; index < count; index++)
  {
    cout << setw (10) << id[index] <<
          setw (10) << score[index] <<
          setw (20) << gradeString[index] << endl;
  } 
}


void calculateGrade (int score[], int id[], string gradeString, int count,
                         float average)
{
  for (int index = 0; index < count; index++)
  {
    if (score[index] < average - 10)
          gradeString[index] = "Unsatisfactory";

    else if (score[index] > average + 10)
          gradeString[index] = "Outstanding";

    else gradeString[index] = "Satisfactory";
  }
}

void openInputFile (ifstream &rss)
{
	string rss2;
	cout << "Please enter in the name of the file that you want to open:" << endl;
	cin >> rss2;

	rss.open (rss2.c_str());

	if (rss.fail())
	{
	cerr <<  "ERROR: cannot open " << rss2
		 <<  " for input." << endl;
	exit(1);
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 07:05 PM

your gradeString is of type
std::string


not
char []



set it equal directly like this
void calculateGrade (int score[], int id[], string gradeString, int count,
                         float average)
{
  for (int index = 0; index < count; index++)
  {
    if (score[index] < average - 10)
          gradeString = "Unsatisfactory";

    else if (score[index] > average + 10)
          gradeString = "Outstanding";

    else gradeString = "Satisfactory";
  }
}


Was This Post Helpful? 1
  • +
  • -

#6 astroshark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 07:08 PM

did that but right when its about to display if it was outstanding, unsatisfactory, or satisfactory it gives me a debug error.

UPDATE: NVM I found out the reason

But now it fully runs but it does not show the outstanding or satisfactory stuff at all.

This post has been edited by astroshark: 30 March 2010 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 07:12 PM

pass that gradeString by reference in all of your functions, should work
Was This Post Helpful? 1
  • +
  • -

#8 astroshark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 07:15 PM

I thank you for your help so far it has worked for me so far but here is my current code right now it just says all the grades are Unsatisfactory any idea?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

void readStudentData (int id[], int score[], ifstream &rss,
								int &count, bool &tooMany, string &gradeString);

float computeAverage (int score[], int count);

void printTable (int id[], int score[], int count, string &gradeString);

void calculateGrade (int score[], int id[], string &gradeString, int count,
                         float average);

void openInputFile (ifstream & rss);

int main()
{
	const int MAX_SIZE = 21;
	ifstream rss;
	int id[MAX_SIZE], score[MAX_SIZE];
	string gradeString;
	int count;
	bool tooMany;

	openInputFile (rss);
	readStudentData (id, score, rss, count, tooMany, gradeString);
	if (tooMany)
	{
	  cout << "More than 21 items so not all could be processed"  << endl;
	}
	printTable (id, score, count, gradeString);

	return 0;
}

void readStudentData (int id[], int score[], ifstream &rss,
								int &count, bool &tooMany, string &gradeString)
{
	int i = 0;
    while(rss >> id[i] >> score[i])
    {
		i++;
		if(i>=21) break;       
	}
	count = i;
  tooMany = false;

  float average = computeAverage (score, count);
  calculateGrade (score, id, gradeString, count, average);
}

float computeAverage (int score[], int count)
{
  float sum = 0, average;

  for (int index = 0; index < count; index++)
  {
    sum += score[index];
  }
  average = sum / count;
  return average;
}

void printTable (int id[], int score[], int count, string &gradeString)
{
	cout << "Student ID" << setw(10) << "Score" << setw(10) << "Grade" << endl;
	for (int index = 0; index < count; index++)
	{
		cout << left << setw (15) << id[index] <<
		setw (10) << score[index] <<
		setw (20) << gradeString << endl;
	} 
}


void calculateGrade (int score[], int id[], string &gradeString, int count,
                         float average)
{
  for (int index = 0; index < count; index++)
  {
    if (score[index] < average - 10)
          gradeString = "Unsatisfactory";

    else if (score[index] > average + 10)
          gradeString = "Outstanding";

    else gradeString = "Satisfactory";
  }
}

void openInputFile (ifstream &rss)
{
	string rss2;
	cout << "Please enter in the name of the file that you want to open:" << endl;
	cin >> rss2;

	rss.open (rss2.c_str());

	if (rss.fail())
	{
	cerr <<  "ERROR: cannot open " << rss2
		 <<  " for input." << endl;
	exit(1);
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 07:26 PM

you need to make your gradeString an array and have an index for each student. Pass the array through your functions with pointers so you can retain the array inside of main()
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

void readStudentData (int id[], int score[], ifstream &rss,
                                                                int &count, bool &tooMany, string *gradeString);

float computeAverage (int score[], int count);

void printTable (int id[], int score[], int count, string *gradeString);

void calculateGrade (int score[], int id[], string *gradeString, int count,
                         float average);

void openInputFile (ifstream & rss);

int main()
{
        const int MAX_SIZE = 21;
        ifstream rss;
        int id[MAX_SIZE], score[MAX_SIZE];
        string gradeString[MAX_SIZE]; //a different string for each person
        int count;
        bool tooMany;

        openInputFile (rss);
        readStudentData (id, score, rss, count, tooMany, gradeString);
        if (tooMany)
        {
          cout << "More than 21 items so not all could be processed"  << endl;
        }
        printTable (id, score, count, gradeString);

		//pause window
		cin.ignore();
		cin.get();
        return 0;
}

void readStudentData (int id[], int score[], ifstream &rss, int &count, bool &tooMany, string *gradeString)
{
        int i = 0;
    while(rss >> id[i] >> score[i])
    {
                i++;
                if(i>=21) break;       
        }
        count = i;
  tooMany = false;

  float average = computeAverage (score, count);
  calculateGrade (score, id, gradeString, count, average);
}

float computeAverage (int score[], int count)
{
  float sum = 0, average;

  for (int index = 0; index < count; index++)
  {
    sum += score[index];
  }
  average = sum / count;
  return average;
}

void printTable (int id[], int score[], int count, string *gradeString)
{
        cout << "Student ID" << setw(10) << "Score" << setw(10) << "Grade" << endl;
        for (int index = 0; index < count; index++)
        {
                cout << left << setw (15) << id[index] <<
                setw (10) << score[index] <<
                setw (20) << gradeString[index] << endl;
        } 
}


void calculateGrade (int score[], int id[], string *gradeString, int count,
                         float average)
{
  for (int index = 0; index < count; index++)
  {
    if (score[index] < average - 10)
          gradeString[index] = "Unsatisfactory";

    else if (score[index] > average + 10)
          gradeString[index] = "Outstanding";

    else gradeString[index] = "Satisfactory";
  }
}

void openInputFile (ifstream &rss)
{
        string rss2;
        cout << "Please enter in the name of the file that you want to open:" << endl;
        cin >> rss2;

        rss.open (rss2.c_str());

        if (rss.fail())
        {
        cerr <<  "ERROR: cannot open " << rss2
                 <<  " for input." << endl;
        exit(1);
        }
}


Was This Post Helpful? 1
  • +
  • -

#10 astroshark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Trying to read ids and scores from text file into an array

Posted 30 March 2010 - 07:32 PM

You are amazing I really do appreciate your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1