Hi all!
Brand new to this stuff but having a ton of fun learning.
Here is my professor's comment about my latest project.
"After the validity of the input file has been verified, the program can read just the first value in the file. After this the loop then will start reading from the second value through to the nth value (the last value). From what has been covered in the course, why will the loop read start at the second value and not the first?"
He also mentioned the following,
"Better to get the initial values for max and min before the loop rather than do a test in each iteration."
Please, bear in mind he found it prudent to give me an "A" on this (104% including extra credit).
I know it has something to do with the if statement test I set up to find the minimum and maximum values in the while loop that is reading the file into the variable num.
Any help, advice, nudge in the right direction will be greatly appreciated!
I just want to be sure I'm not completely missing out on an important nuanced idea about this stuff.
Below is the code;
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/******************************
CS M10A FALL2016
Topic D Program & Extra Credit
TopicD.cpp
Status: in progress
*******************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName;
cout << "Enter a file name: ";
cin >> fileName;
ifstream inputFile;
inputFile.open(fileName);
while (!inputFile)
{
cout << "\nFilename not valid." << endl;
cout << "Please enter a different name: ";
cin >> fileName;
inputFile.open(fileName);
}
int count = 0,
average = 0,
signal = 0,
sum = 0,
num,
minVal,
maxVal;
while (inputFile >> num)
{
if (signal++ == 0) // here is the issue (read starts from second value)
{
minVal = num;
maxVal = num;
} // ends here
if (num < minVal)
minVal = num;
else if (num > maxVal)
maxVal = num;
sum += num;
average = sum / ++count;
}
inputFile.close();
char userChoice;
do
{
cout << "\nMake a selection from the list\n" << endl;
cout << left << "A. Get the largest value" << endl;
cout << left << "B. Get the smallest value" << endl;
cout << left << "C. Get the sum of the values" << endl;
cout << left << "D. Get the average of the values" << endl;
cout << left << "E. Get the number of values entered" << endl;
cout << left << "F. Exit the program\n" << endl;
cout << left << "\nEnter your choice --> ";
cin >> userChoice;
switch (userChoice)
{
case 'a':
case 'A': cout << "\n\n\nThe largest value is " << maxVal << "\n\n\n";
break;
case 'b':
case 'B': cout << "\n\n\nThe smallest value is " << minVal << "\n\n\n";
break;
case 'c':
case 'C': cout << "\n\n\nThe sum of the values is " << sum << "\n\n\n ";
break;
case 'd':
case 'D': cout << "\n\n\nThe average of the values is " << average << "\n\n\n";
break;
case 'e':
case 'E': cout << "\n\n\nThe number of values entered is " << count << "\n\n\n";
break;
case 'f':
case 'F': cout << "\n\nProgram ending\n\n"
<< "\nPress enter to end";
break;
default: cout << "\n\n\n*****That is not a valid entry*****\n\n\n";
break;
}
}
while (userChoice != 'f' && userChoice != 'F');
cin.ignore();
cin.get();
return 0;
}
| |