#include <fstream>
#include <iostream>
usingnamespace std;
int main()
{
// declare variables
int score, totalScore = 0, count = 0; // count is count of students(or scores)
int numA = 0, numB = 0, numC = 0, numD = 0, numU = 0; // number of As, ..., etc
ifstream input;
ofstream output;
// open file scores.dat for read.
// if open file for read fails, print out error message and exit the program (see demo for details)
input.open("scores.dat");
if(input.fail())
{
cout << "Unable to open file scores.dat to read";
system("pause");
exit(1);
}
// as long as there is a score in file to read
while(input >> score)
{
// read a score from
input >> score;
// update total score
totalScore = totalScore + score;
// update the number of As, Bs, Cs, Ds, and Us
switch(score/10)
{
case 10: case 9:
numA++;
break;
case 8:
numB++;
break;
case 7:
numC++;
case 6:
numD++;
default:
numU++;
break;
}
// update the count of students
count++;
}
// if count of students is 0, print out error message and exit program
if(count == 0)
{
cout << "Student count is 0. Program exit.";
}
else
{
// open file YourName.txt for write
output.open("MounesJeremy.txt");
// set correct ios flags
output.setf(ios::fixed);
output.setf(ios::showpoint);
output.precision(2);
// output result to the file
output << "\n\n\t\tTest Statistics";
output << "\n\n\tTotal number of students: " << count;
output << "\n\tNumber of A's: " << numA;
output << "\n\tNumber of B's: " << numB;
output << "\n\tNumber of C's: " << numC;
output << "\n\tNumber of D's: " << numD;
output << "\n\tNumber of U's: " << numU;
}
cout << "\n\tThe test stats were output to MounesJeremy.txt";
cout << "\n\tThank you for using MyTeachingAssistant software\n\t";
// close files
input.close();
system("pause");
return 0;
}
The problem is, while this method seems to work, when a list of 26 integers is given:
1 2 3 4 5 6 7 8
Test Statistics
Total number of students: 13
Number of A's: 4
Number of B's: 3
Number of C's: 2
Number of D's: 5
Number of U's: 6
What's happening there is it does the read and then inputitself is used as the condition. Using an instance of ifstream or ofstream as a condition is basically the same thing as saying myStreamInstance.good().
I wasn't aware the declaration of a do/while loop allowed for operations of any kind. I thought while loops were conditional only, with for loops reserved for that type of thing.
Would this work, then?
while(i++)
{
do something
if(i > 5) { break; }
}
Mark as solved, yes I am awar of the dumb stuff in that program, such as the missing break;'s