Really loving how the body is already preset with "write your question here" and the proper flag for our code.
ANYWAY!!
I'm not necessarily having an issue (great way to discourage help). I just want to better understand the use of cin, the keyboard buffer, and the interaction of variables with their input types.
The subject for today is inputting a char into an int variable and the craziness that rapidly ensues at the output. The focus is the "readTestandHwScore" function.
I'm observations:
- nothing gets assigned during the first iteration
- all subsequent iterations skip the "cin" statement
- I am somehow getting two iterations before the system("pause") stops it from overflowing.
So, I would love to know how to prevent this. I've seen a few threads recommend cin.fail (i think that's the call out anyway). But i'm more curious as to why it do what it do when it fails.
My assumptions:
- the first term doesn't accept anything
- The keyboard buffer then doesn't clear
- iteration two gets assigned a random value
- ???
- profit
Contents of Interest:
Line 32 - "readTestandHwScore" function
Line 40 - do-while input loop.
Current Output:
Let's figure out if your passing.
What were your scores for this term:
Test 1: a
Press any key to continue . . . [ENTER]
Test 2:
No way you have a 1.2659e+231
Try that again...
Press any key to continue . . . [NO INPUT]
Test 2:
No way you have a 1.2659e+231
Try that again...
Press any key to continue . . . [ENTER]
Test 2:
No way you have a 1.2659e+231
Try that again...
Press any key to continue . . . [NO INPUT]
Test 2:
No way you have a 1.2659e+231
Try that again...
Press any key to continue . . . [Stopped program through eclipse]
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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// List of Functions:
void readTestandHwScore (double &, double &, double &);
void printFinalScore (double);
double calcFinalScore (double, double, double);
int main (){
double scoreTEST1,
scoreTEST2,
scoreHW;
cout << "Let's figure out if your passing."
<< endl << endl;
cout << "What were your scores for this term:"
<< endl;;
readTestandHwScore(scoreTEST1, scoreTEST2, scoreHW);
printFinalScore(calcFinalScore(scoreTEST1, scoreTEST2, scoreHW));
return 0;
}
void readTestandHwScore (double &test1, double &test2, double &hw)
{
int check;
double scoreARRAY[3];
string scoreTYPE[]{ "Test 1", "Test 2", "Homework"};
for (check = 0 ; check < 3 ; check++)
{
do {
cout << scoreTYPE[check]<< ": ";
cin >> scoreARRAY[check];
if(scoreARRAY[check] < 0)
cout << endl
<< "A negative score is not possible\n"
"Please re-enter..."
<< endl;
else if(scoreARRAY[check] > 100)
cout << endl
<< "No way you have a "<< scoreARRAY[check]
<< "\nTry that again..."
<< endl;
system("pause");
} while (scoreARRAY[check] < 0 || scoreARRAY[check] > 100);
}
test1 = scoreARRAY[0];
test2 = scoreARRAY[1];
hw = scoreARRAY[2];
}
void printFinalScore(double avg)
{
// Evaluate scoreAVG for it's corresponding letter grade
// A: >=90 B: >=80 C: >=70 D: >=60 F: <60
char scoreLETTER[]{ 'F', 'D', 'C', 'B', 'A'};
int scoreCOMP[]{0, 60, 70, 80, 90},
set,
check;
cout << setprecision(2) << fixed << showpoint << right;
cout << "Your average is: " << avg
<< endl;
for(check = 0 ; check < 5 ; check++)
{
if (avg >= scoreCOMP[check])
set = check;
}
cout << "You have a "<< avg << " - " << scoreLETTER[set];
}
double calcFinalScore(double test1, double test2, double hw)
{
double scoreAVG = 0;
// Test scores are 40% and Homework is 20%
scoreAVG = (test1 + test2)*0.4 + (hw*0.2);
return scoreAVG;
}
|