Increment Increasing Even Though It Shouldn't?

I am a bit stumped as to why this happening but I am testing to see if a user's input is a whole number and if it falls within a particular number range. The code is working right, in that it asks the user to reenter their number if it doesn't match the criteria, except that when they enter in a decimal the code continues to increase the increment.

I believe that when I clear the flags, the conditional is reevaluated and the decimal number passes the else clause but I don't get why the break doesn't restart me at the beginning of the while.

What am I missing?

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string si;
    string GradePrompt;
    string ScoreInput;
    int i;
    int e;
    int ExamScore;
    int TotalPoints;
    int InputLength;
    bool GetInput;
    stringstream ss;
    stringstream es;
    double CourseScore;
    char FinalGradeLetter;
    double FinalPercentage;

    i = 0;
    e = 1;
    TotalPoints = 0;

    while (e < 6)
    {
        GetInput = true;
        while ((GetInput == true))
        {
            ss << e;
            si = ss.str();
            GradePrompt = "Enter Exam " + si + " Score :";
            ss.str("");
            cout << GradePrompt << '\n';
            cin >> ExamScore;

            if (cin.fail())
            {

                cout << "Invalid Score.  Please Enter Whole Numbers Only." << '\n';
                cin.clear();
                cin.ignore(1000, '\n');
                break;
            }

            else
            {
                if ((ExamScore >= 0) && (ExamScore <= 100))
                {
                    TotalPoints = TotalPoints + ExamScore;
                    GetInput = false;
                    ++e;
                }
                else
                {
                    cout << "Invalid Score. Scores must range between 0 and 100" << '\n';
                    break;
                }
            }
        }
    }


    cout << "Total Exam Points: " << TotalPoints << '\n';
    CourseScore = TotalPoints / 500.00;
    {
        FinalPercentage = (CourseScore * 100);
        cout.precision(1);
        cout << fixed;
    }

    if (FinalPercentage >= 90)
    {
        FinalGradeLetter = 'A';
    }
    else if (FinalPercentage >= 80)
    {
        FinalGradeLetter = 'B';
    }
    else if (FinalPercentage >= 70)
    {
        FinalGradeLetter = 'C';
    }
    else if (FinalPercentage >= 60)
    {
        FinalGradeLetter = 'D';
    }
    else
    {
        FinalGradeLetter = 'F';
    }

    cout << "Placeholder" << ", " <<"Placeholder" << '\t' << FinalGradeLetter << " (" << TotalPoints << "/500 = " << FinalPercentage << "%)" << '\n';
    system("PAUSE");
}    
Hello, when an integer is assigned a value with a decimal point it will change it into a integer value. For example
1
2
 int a = 0.44
std::cout << a;

Would print 0.
So when a decimal is entered into int ExamScore it will just remove everything after the decimal point. So when this part of your code executes
1
2
if ((ExamScore >= 0) && (ExamScore <= 100))
                {
it will evaluate to true and set bool GetInput; to false.
Topic archived. No new replies allowed.