Count-Controlled Loop

We have a project that is supposed to open an input file and output student names, scores, averages, etc. to an output file. I've encountered a problem with one of my count-controlled while loops. The way i have it written currently, it performs the proper number of iterations, but if I change (hwNum <= 4) to (hwNum < 4) I get some very strange output (there are only 4 HW scores so this loop needs to be hwNum < 4). It allows me to manipulate either one of my other count-controlled loops, but if I change my initialization value or my restriction for this loop, my number of iterations and output go haywire. Any help would be greatly appreciated.

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

using namespace std;

int main ()
{
ifstream inFile;
ofstream outFile;
string input, output;
char yeah;
string last, first;
int q1Tot, q2Tot, q3Tot, q4Tot, hw1Tot, hw2Tot, hw3Tot, hw4Tot, exam1Tot, exam2Tot;
int quizTot, hwTot, examTot, pointsTot;
int studentNum=0, quizNum=0, hwNum=0, examNum=0;
int quiz, hw, exam, quizSum=0, hwSum=0, examSum=0;

cout << "Enter in the name of the input file: ";
cin >> input;
cout << input<< endl;

inFile.open(input.c_str());

while (!inFile)
{
cout << "\n" << string(15, '*') << " File Open Error ";
cout << string(15, '*') << endl;
cout << "==> Input file failed to open properly!!\n";
cout << "==> Attempted to open file: " << input << endl;
cout << string(47,'*')<< endl;

inFile.clear();

cout << "\nEnter in the name of the input file: ";
cin >> input;
cout << input<< endl;
inFile.open(input.c_str());
}

cout << "Enter in the name of the output file: ";
cin >> output;
cout << output<< endl;

outFile.open(output.c_str());

while (!outFile)
{
cout << "\n" << string(15, '*') << " File Open Error ";
cout << string(15, '*') << endl;
cout << "==> Output file failed to open properly!!\n";
cout << "==> Attempted to open file: " << output << endl;
cout << string(47,'*')<< endl;

outFile.clear();

cout << "\nEnter in the name of the output file: ";
cin >> output;
cout << output<< endl;
outFile.open(input.c_str());
}

inFile >> last;

if(!inFile)
{
cout << "\nFile is empty, ending program" << endl;
outFile << "File is empty, ending program" << endl;
cin >> yeah;
return 1;
}
else
{

outFile << left << setw(3) << "#" << left << setw(12) << "Last" << left << setw(7) << "First";
outFile << left << setw(6) << "Quiz" << left <<setw(6) << "HW" << left << setw(6) << "Exam";
outFile << left << setw(7) << "Total" << left << setw(9) << "Average" << endl;
outFile << left << setw(3) << "-" << left << setw(12) << "----------" << left << setw(7) << "-----";
outFile << left << setw(6) << "----" << left << setw(6) << "---" << left << setw(6) << "----";
outFile << left << setw(7) << "-----" << left << setw(9) << "-------" << endl;

inFile >> first >> q1Tot >> q2Tot >> q3Tot >> q4Tot >> hw1Tot >> hw2Tot >> hw3Tot >> hw4Tot >> exam1Tot >> exam2Tot;

quizTot = q1Tot + q2Tot + q3Tot + q4Tot;
hwTot = hw1Tot + hw2Tot + hw3Tot + hw4Tot;
examTot = exam1Tot + exam2Tot;
pointsTot = quizTot + hwTot + examTot;


outFile << left << setw(3) << " " << left << setw(12) << last << left << setw(7) << first;
outFile << left << setw(6) << quizTot << left <<setw(6) << hwTot << left << setw(6) << examTot;
outFile << left << setw(7) << pointsTot << left << setw(9) << "100.00" << endl;
outFile << left << setw(3) << "-" << left << setw(12) << "----------" << left << setw(7) << "-----";
outFile << left << setw(6) << "----" << left << setw(6) << "---" << left << setw(6) << "----";
outFile << left << setw(7) << "-----" << left << setw(9) << "-------" << endl;

}


inFile >> last;
while (inFile)
{

inFile >>first;
studentNum++;


while (quizNum < 4)
{

inFile >> quiz;
quizSum += quiz;
quizNum++;
}


while (hwNum <= 4)
{

inFile >> hw;
hwSum += hw;
hwNum++;
}


while (examNum < 2)
{

inFile >> exam;
examSum += exam;
examNum++;
}


}




outFile << studentNum << " " << last << " " << first << " " << quizSum;
outFile << " " << hwNum << " " << examNum;
cin >> yeah;



return 0;
}

P.S. The format of the input file is a key line that consists of "Key Key 10 10 10 10 20 20 20 20 100 100" where the first 4 numbers are quiz scores, second 4 are homework scores, and the last 2 are exam scores. The following lines follow the same format but have various first and last names in place of "key key." Sorry for the lengthy post :/

First of all use formating of the code.

I see that the loop you are speaking about is inside another outer loop while( inFile ). Your shall initilaize quizNum, hwNum, and examNum each time when the outer loop is repeated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
while (inFile)
 {

 inFile >>first;
 studentNum++; 


while (quizNum < 4)
 {

 inFile >> quiz;
 quizSum += quiz;
 quizNum++;
 }


 while (hwNum <= 4)
 {

 inFile >> hw;
 hwSum += hw;
 hwNum++;
 }


 while (examNum < 2)
 {

 inFile >> exam;
 examSum += exam;
 examNum++;
 }

 }


So it would be more correctly if you would include the following statement in the outer loop

quizNum = hwNum = examNum = 0;
Last edited on
Topic archived. No new replies allowed.