Purchase Solution

Infinite Loop C++ Programs

Not what you're looking for?

Ask Custom Question

Can you find a bug in this program and then fix it so that it won't have an infinite loop?

#include <iostream>
#include <fstream>

using namespace std;

void CalculateAvgScore(ifstream &fIn,ofstream &fOut);

int main(int argc, char *argv[])
{
ifstream fin;
ofstream fout;

char inFileName[20],outFileName[20];

cout << "Enter the name of an existing text file: ";
cin>>inFileName;

cout << "Enter a name for output text file: ";
cin>>outFileName;

fin.open(inFileName,fstream::in);//open the input file
fout.open(outFileName,fstream::out);//open the ouuput file

CalculateAvgScore(fin,fout);//add one more field avgscore to the output file

fin.close();//close the input file
fout.close();//close the output file

system ("PAUSE");
return 0;
}

void CalculateAvgScore(ifstream &fIn,ofstream &fOut)
{
while (!fIn.eof())
{
char firstName[30],lastName[30];
int Scores[10]={0};
int totalScore=0;
double avgScore=0;

fIn>>lastName;//read lasttname
fIn>>firstName;//read firstname

if (lastName[0]=='') continue;//skip empty lines

for (int i=0;i<10;i++)
{
fIn >> Scores[i];//read score one by one
totalScore += Scores[i];
}

avgScore = (double)totalScore / 10;//calculate avg score

fOut << lastName;//write lastname
fOut << " ";//write one space

fOut << firstName;//write firstname
fOut << " ";

for (int j=0;j<10;j++)
{
fOut << Scores[j];//write score one by one
fOut << " ";
}

fOut << avgScore;//write avg score
fOut << "n";//write one change line character
}
}

Purchase this Solution

Solution Summary

The expert examines infinite loop C++ programs. The average score is calculated.

Solution Preview

You should make sure that the file name is correct and input file is open before you call the while loop. I have added a line
if (!fin) {cerr<<"File not ...

Purchase this Solution


Free BrainMass Quizzes
C++ Operators

This quiz tests a student's knowledge about C++ operators.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

C# variables and classes

This quiz contains questions about C# classes and variables.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.