1

The program requires the user to input numbers and when finished to exit the loop by entering a negative number. The program will then output the average of the numbers and the count of the numbers. The negative number should be removed from the series though. So if we have three numbers and exit on the (fourth) negative number, the average will only be of the three numbers and not include the negative number. Neat, I somehow made that work. Now, I need to expand upon this to make it so that the input is verified to be an integer and less than 100 also using a Boolean equation. It is at this point that I cannot determine how to exclude the erroneous input from the results.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    float inScore= 1;
    float sumScore= 0.0;
    int scoreCount= 0;
    float avgScore= 0.0;
    bool moreNumbers = true;
    bool notNumbers = true;


    for ( inScore =1; inScore >=1; inScore++)  //for loop some kind of blackmagic not really sure about this right now
    {
        cout << "Please enter grades (enter a negative integer to exit): ";
        cin >> inScore;
        if (!cin || inScore > 100)
        {
            notNumbers = false;  //boolean to ignore non integers or numbers greater than 100
            cin.clear();
            cin.ignore(256, '\n');
            scoreCount--;
            cout << "Invalid number!" << endl;
        }         //something is wrong in this section and I can't get it to ignore the invalid inputs

        else if (inScore < 0)
        {
            moreNumbers = false;
        }         //breaks the loop upon a negative number being entered
        else
            sumScore+=inScore;
        scoreCount++;
    }
    avgScore = (sumScore+=inScore)/(scoreCount-1);
    cout << "Number of Grades: " << scoreCount-1 << endl;  //number of entries
    cout << "Average: " << avgScore << endl;                // average grade except I cannot figure out how to remove negative number from the array

    return 0;
}
CSW4213
  • 5
  • 4
  • Don't you need specify n? `istream& ignore (streamsize n = 1, int delim = EOF);` – Soumya Kanti Nov 12 '18 at 03:55
  • I see the comment after `for ( inScore =1; inScore >=1; inScore++)`, but if you're going to use junk code as a place holder, try to use something that makes sense. This only serves to obscure your problem. – user4581301 Nov 12 '18 at 04:16
  • Suggestion: Don't BS code, write less code. Right now what you want to figure out is how to get a valid number and only a valid number. To do that, write a function that does exactly that and nothing else. Then write a simple `main` function that tests the function every way you can think of. Once you have the function working, worry about looping calls to the function until you hit the exit condition. Think small and then bolt the small things together into something big. – user4581301 Nov 12 '18 at 04:17
  • [Totally unrelated](https://www.youtube.com/watch?v=q8IHMctrKCg). But to get back on topic, `cin.ignore();` removes only one character. You need to remove the whole bad input and probably the whole line. To do that you'll want `cin.ignore(numeric_limits::max(), '\n')` and `#include ` to get `max`. – user4581301 Nov 12 '18 at 04:21
  • I changed the cin.ignore to reflect the size of the input and added "scoreCount--;" to remove the erroneous input. It seems to give the intended results at this point. Thanks @user4581301 – CSW4213 Nov 12 '18 at 04:39

0 Answers0