I'm using Visual Studio. No Errors are declared, but this will not display any of my menu. I think it is because the while(inputfile) meant to measure the integer values and count them is in some kind of loop.
/*
<John P>
CS M10A
Topic D Project First Program
TopicD.cpp
Status: Working
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
// Variable Declarations
int integer, // integer in file
lrg_int = -32767, // the largest integer
sml_int = 32767, // the smallest integer
sum_int = 0, // sum of integers
num_int = 0, // number of integers
avg_int; // average of integers
// Read Text File
ifstream inputFile;
inputFile.open("random.txt");
// Display error if file doesn't exist
if (!inputFile)
{
cout << "Cannot open the file random.txt /n";
return 0; // Closes program after attempt to open file fails.
}
// Read the integers from inputfile random.txt
inputFile >> integer;
while (inputFile)
{
num_int++; // Increments number of integers in file
sum_int = sum_int + integer; // Determines running total of integers in file
if (integer > lrg_int) // Replaces lrg_int with the largest running integer
lrg_int = integer;
if (integer < sml_int) // Replaces sml_int with the smallest running integer
sml_int = integer;
}
// Menu Creation
char choice;
do
{
// Display Menu
cout << "Make a selection from the list \n"
<< "A. Get the largest value \n"
<< "B. Get the smallest value \n"
<< "C. Get the sum of the values \n"
<< "D. Get the average \n"
<< "E. Get the number of values entered \n"
<< "F. End this program \n \n \n";
cin >> choice;
// Validate Menu Selection
while (!(choice >= 'A' && choice <= 'F' || choice >= 'a' && choice <= 'f'))
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
// Process User's Choice
switch (choice)
{
case'A' :
case'a' : cout << "The largest value is " << lrg_int << endl;
break;
case'B' :
case'b' : cout << "The smallest value is " << sml_int << endl;
break;
case'C' :
case'c' : cout << "The sum of the values is " << sum_int << endl;
break;
case'D' :
case'd' : avg_int = sum_int / num_int;
cout << "The average of the values is " << avg_int << endl;
break;
case'E' :
case'e': cout << "The number of values is " << num_int << endl;
break;
}
} while (choice != 'F' || choice != 'f');
// First requests then registers enter key in order to end program.
cout << "Program Over \n \n \n \n";
cout << "Press Enter to end --> \n \n \n";
cin.get(); // Pressing the enter key here ends the program
return 0;
}
I do not know how to attach text files on this forum. I keep the text file in the inner folder of visual studio with my program. It is entitled random.txt. Here is its contents: