/******************************************************************************************** histo1.cpp Jim Millard for CO211/Fall 1999 Program that tracks integer input and displays a histogram of specific results. ********************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; int main() { const char filename[] = "histogrm.txt"; //this can be later changed to allow user input int current, i; int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, countOther = 0; //inform and prompt the user cout << endl << "This program will take a set of integers as input and" << endl << "after counting the instances of the numbers 1..5 respectively, as" << endl << "well as the count of integers not in that range, displays a histogram" << endl << "of the results" << endl; cout << "The program will return the results when you enter [Ctrl-D] as input." << endl; //prompt and extract the rest of the set cout << "Enter a set of numbers: "; while (cin >> current) { switch (current) //pigeonhole input and accumulate counts { case 1: count1++; break; case 2: count2++; break; case 3: count3++; break; case 4: count4++; break; case 5: count5++; break; default: countOther++; } //switch } //while //output the results cout << endl; ofstream outfile(filename); if (outfile) { outfile << "Results:" << endl << "1 "; for (i=0; i < count1; i++) outfile << '*'; outfile << endl << "2 "; for (i=0; i < count2; i++) outfile << '*'; outfile << endl << "3 "; for (i=0; i < count3; i++) outfile << '*'; outfile << endl << "4 "; for (i=0; i < count4; i++) outfile << '*'; outfile << endl << "5 "; for (i=0; i < count5; i++) outfile << '*'; outfile << endl << "o "; for (i=0; i < countOther; i++) outfile << '*'; outfile << endl; return 0; } else { cerr << "Unable to open \"" << filename << "\" for writing results!" << endl; return -1; } }