4 Replies - 411 Views - Last Post: 30 November 2012 - 01:18 PM Rate Topic: -----

#1 jvesley  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Problem with File I/O using Nested For Loops

Posted 29 November 2012 - 04:05 PM

I'm trying to read the first and last names, and six separate scores out of an input text file for seven different individuals (see attached 'input.txt' with fairy tale character's s names). The first three scores are supposed to be considered 'program' scores and the last three are supposed to be 'test' scores (scores on program assignments and class tests) . We are using those numbers to calculate a program average, a test average, and a course average. I'm using one function containing an end of file while loop to execute three functions (input the data, calculate the averages, and output the data- all using their own 'for' loops). You'll have to excuse some things being out of place, the code is messy because I've been trying so many different things to make the calculations work. The file input function works fine. The problem is doing calculations on the input data. I've been trying to use a for loop to do the calculations for the program and test averages, but I can't figure out the right way to do it for the life of me. Either I get error messages or an improper output. Perhaps there's another type of loop I could be using. Or maybe I should declare two separate arrays for program and test scores. I know I could just do the long form, counting the spots in the array individually and just doing all the calculations and output inside of one EOF while loop, but I'm trying to be fancy and learn how to use for loops with functions. I'm really confused:


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

//Global Infile and Outfile
ifstream fin;
ofstream fout;

//Function Prototypes
void eofwhileloopfn(ifstream &fin, ofstream &fout,
string name[2],float score[6],float avg[3]);
void inputfn(ifstream &fin,string name[2], float score[6]);
void calcfn(float score[6],float avg[3]);
void outputfn(ifstream &fin, ofstream &fout,
string name[2],float score[6], float avg[3]);

int main ()
{
system ("color f0");
string name[2];
float score[6], avg[3];
char inputfile[51];//stores name of the input file
char outputfile[51];//variable to store the name of the output file 
cout<<left<<fixed<<showpoint<<setprecision(2);
cout<<"Enter the input file name: ";
cin>>inputfile;
cout<<endl;
fin.open(inputfile);
if (!fin)
{
cout<<"Cannot open the input file. "<<endl;
system("pause");
return 1;
}
cout<<"Enter the output file name: ";
cin>>outputfile;
cout<<endl;
fout.open(outputfile);

// The End-of-File While Loop Function 
eofwhileloopfn (fin,fout,name,score,avg);

fin.close();
fout.close();
system ("pause");
return 0;
}//end of main fn


//***********************************************************************

void eofwhileloopfn(ifstream &fin, ofstream &fout,
string name[2],float score[6], float avg[3])
//float sum
{

//End-Of-File Controlled While Loop
//Call Other Functions Inside of Loop
while (!fin.eof())
{
inputfn(fin,name,score);
calcfn (score,avg);
outputfn(fin,fout,name,score,avg);
} 
}//End of Loop Function 

//************************************************************************
void inputfn(ifstream &fin, string name[2], float score[6])
{
//File Input
fin>>name[0]>>name[1];
for (int col=0;col<6;col++)
{fin>>score[col];}//col represents index
}

//***********************************************************************

void calcfn(float score[6],float avg[3])
//int sum [2]
{ 
cout<<left<<fixed<<showpoint<<setprecision(2);
//Program Average
//Test Average
for (int col=0;col<6;col++)
{
while (col<3)    
//for (int col=0;col<3;col++)
{
avg[0]=float(score[col]+score[col]+score[col]);
}
while (col>3||col<6)
//for (int col=0;col<6;col++)
{
avg[1]=float(score[col]+score[col]+score[col]);
}
}
//Course Average
avg[0]=avg[0]/3.0;
avg[1]=avg[1]/3.0;
avg[2]=(avg[0]+avg[1])/2.0;
}//end of calculation function 

//***********************************************************************
void outputfn(ifstream &fin, ofstream &fout,
string name[2],float score[6],float avg[3])
{
cout<<left<<setw(20)<<name[0]+" "+name[1];
for (int col=0;col<3;col++)
{cout<<left<<setw(5)<<avg[0]<<setw(5)<<avg[1]<<setw(5)<<avg[2];}
cout<<endl;
}//End of Output Function 

//***********************************************************************




Please ignore the "//float sum" 's, I was just trying to use those to play around with the calculations.

Attached File(s)

  • Attached File  input.txt (258bytes)
    Number of downloads: 25


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with File I/O using Nested For Loops

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Problem with File I/O using Nested For Loops

Posted 29 November 2012 - 07:56 PM

I can't help you with the C++ style, but to debug it:

1) Verify your input by having the data printed on screen, with a getchar() afterward to make sure you can see it before it scrolls off the screen.

2) Verify your sums, again, have the sums printed on screen, and add a getchar() to hold the screen long enough that you can ascertain it's accuracy.

3) Verify the average. Same as above.

It's no good to just have a lot of code and a nice logic to them. You have to be able to debug it, as well - and that takes practice.
Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,625
  • Joined: 19-February 09

Re: Problem with File I/O using Nested For Loops

Posted 29 November 2012 - 08:52 PM

You will probably need to initialize the averages to zero to start with.

To update a value in a loop you can do it in the form of, new_value = old_value + added_value, where new_value and old_value are the same variable.

  for(...)  {

    average[] = average[] + score[];
  }


Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Problem with File I/O using Nested For Loops

Posted 30 November 2012 - 07:14 AM

Holy crap, man, indent! Also, no globals. Strangely, your code doesn't even need them.

Your big problem is you while. There are a number of ways to skin this cat. It's so short your really don't need a loop, which might be easiest:
void calcfn(float score[6], float avg[3]) {
	avg[0] = (score[0] + score[1] + score[2]) / 3.0;
	avg[1] = (score[3] + score[4] + score[5]) / 3.0;
	avg[2] = (avg[0] + avg[1]) / 2.0;
}



However, if you wanted a loop...
void calcfn(float score[6], float avg[3]) {
	for(int i=0; i<2; i++) { avg[i] = 0; } // make sure it's zero
	for (int col = 0; col < 6; col++) {
		avg[col / 3] += score[col];
	}
	avg[0] = avg[0] / 3.0;
	avg[1] = avg[1] / 3.0;
	avg[2] = (avg[0] + avg[1]) / 2.0;
}
[code]

Your names are pretty cryptic.  I'd start this like: [code]

const int SCORE_COUNT = 6;
const int AVG_COUNT = 3;

struct Item {
	string lastname, firstname;
	float score[SCORE_COUNT];
	float avg[AVG_COUNT];
};

bool read(istream &, Item &);
void calcAvg(Item &);
void write(ostream &, const Item &);

int process(istream &, ostream &);
int process(const string &filenameIn, const string &filenameOut);

int main() {
	string inputfile, outputfile;
	cout << "Enter the input file name: ";
	cin >> inputfile;
	cout << endl;
	cout << "Enter the output file name: ";
	cin >> outputfile;
	return process(inputfile, outputfile);
}



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#5 jvesley  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: Problem with File I/O using Nested For Loops

Posted 30 November 2012 - 01:18 PM

Thanks for the coaching, Adak. It's always good to remember the basic process. And #define, thanks for reminding me of something crucial I should have remembered. And Baavgai, thanks for BLOWING MY MIND WITH AWESOME CODE. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1