1

I'm having a problem with the input error.My input is validated but my error message "Invalid Range. Please Enter Range 0-100:" is not showing if there's invalid input.

Here's an example:

Please Enter Grade 1: 101 (It does not accept input)
Please Enter Grade 1: 321 (It does not accept input)

Instead of showing:

Please Enter Grade 1: 101 (It does not accept input)

Invalid Rage. Please Enter Range 0-100.
Please Enter Grade 1: 

Here's My Code:

for(vector<double>::size_type i = 0; i < 15; i++)
  {
    do
    {
         cout << "Please Enter Grade "<< i + 1 <<": " << flush;
            cin >> gradesVector[i];
        }
        while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0);
        { 
            cout << "Invalid Rage. Please Enter Range 0-100:\n";
                cout << "Please Enter Grade "<< i + 1 <<": " << flush;
                    cin >> gradesVector[i];
         }   
}
Tayvion Payton
  • 75
  • 1
  • 1
  • 9

3 Answers3

1

Your while() condition should be

while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0) and use this code

for(vector<double>::size_type i = 0; i < 15; i++)
  {
   int c=0;
    do
    {
        if(c!=0)   //checking that we didnt entered loop for first time
          cout << "Invalid Rage. Please Enter Range 0-100:\n";         

           cout << "Please Enter Grade "<< i + 1 <<": " << flush;
           cin >> gradesVector[i];
           c++;   
     }
        while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0);

}
Tanuj Yadav
  • 1,259
  • 13
  • 21
1

You are mixing a do construct with a while. Use either not both. Also your test in your while does not make sense. It can't be less and more at the same time. Edit to the following and it should work

for(vector<double>::size_type i = 0; i < 15; i++)
    {
        cout << "Please Enter Grade "<< i + 1 <<": " << flush;
        cin >> gradesVector[i];
        while(gradesVector[i] < 0.0 || gradesVector[i] > 100.0); // or not and
        { 
            cout << "Invalid Rage. Please Enter Range 0-100:\n";
                cout << "Please Enter Grade "<< i + 1 <<": " << flush;
                    cin >> gradesVector[i];
        }   
    }
MotKohn
  • 3,485
  • 1
  • 24
  • 41
1

Your while condition is not right.

std::vector<double> gradesVector;
gradesVector.reserve(15);
for (std::vector<double>::size_type i = 0; i < 15; i++)
{
    double grade;
    std::cout << "Please Enter Grade " << i + 1 << ": " << std::flush;
    std::cin >> grade;
    while (grade < 0.0 || grade > 100.0)
    {
        std::cout << "Invalid Range. Please Enter Range 0-100:\n";
        std::cin >> grade;
    }
    gradesVector.push_back(grade);
}

Also I would only add the grade to the vector once it is valid.

MarkoPaulo
  • 474
  • 4
  • 19