0

I have tried to run this code but every time I try, the program finishes before the user can answer the question of displaying positive or negative integers. What am I doing wrong?

#include <iostream>

using namespace std;



int main() {
    int number;
    int positiveCount = 0;
    int negativeCount = 0;
    for (int i = 0; i < 10; i++) {
        cin >> number;
        if (number > 0) {
            positiveCount++;
        }
        if (number < 0) {
            negativeCount++;
        }
    }
    char response;
    cout << "Do you want the (p)ositive or (n)egative count? ";
    cin >> response;
    if (response == 'p') {
        cout << "Positive count is " << positiveCount << "\n";
    }
    if (response == 'n') {
        cout << "Negative count is " << negativeCount << "\n";
    }
}
  • 5
    It seems to work okay...https://wandbox.org/permlink/DG68cY2DKo3Pw6kx Can you be more specific about what your inputs and outputs are? – AndyG Jun 22 '21 at 19:56
  • 2
    I suspect `std::cin` is put into an error state while reading numbers. What **should** your program do when a user enters `Phillip` as a one of the numbers? Do you know what your program will currently do? – Drew Dormann Jun 22 '21 at 19:59
  • 2
    @DavidC.Rankin Can you please point me to the sentence, where the CoC says anything about (not) downvoting bad content? Not only that the question is missing to provide a [mcve] as required, it's also likely to end up as a dupe of [this](https://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it), after everything was clarified with the OP. I don't see much value here, hence I down and close voted. – πάντα ῥεῖ Jun 22 '21 at 20:17
  • 1
    @PhillipGay , Your code works fine for me. Please describe the specific issue you have. BTW, if users enter zero, do you want to count it as neither positive nor negative ? – Job_September_2020 Jun 22 '21 at 20:18
  • 1
    @DavidC.Rankin I am here to do curation of the c++ tag / repository. This will help to keep the quality up to the policies the community decided to be the guideline. So how you can say I am _"not here to help"_?!? Stack Overflow isn't a personal helpdesk, and never was one. – πάντα ῥεῖ Jun 22 '21 at 20:23
  • @PhillipGay, I think I may have know the cause of your issue. Did you enter 11 or more integers on the same line instead of 10 integers and then press ENTER once after you enter those 11 integer ? If yes, then you can look at the answer by "Drew Dormann". That will probably fix your issue. – Job_September_2020 Jun 22 '21 at 20:26
  • 2
    imho it is not correct to describe downvotes as aggressive or negative behavior, and I also didnt find anything in the coc that mentions downvotes. Frankly, I believe what went wrong here is that someone was asked to reveal his vote and then did so, such discussions usually lead nowhere – 463035818_is_not_an_ai Jun 22 '21 at 20:30

1 Answers1

2

Your program is well-formed assuming a well-behaved user.

It does not handle unexpected input however. Change:

    cin >> number;
    if (number > 0) {
        positiveCount++;
    }
    if (number < 0) {
        negativeCount++;
    }

To something that handles invalid input:

    while ( ! cin >> number )
    {
        cout << "Invalid input.  Please try again.\n";
        // Clear error flags and ignore input from this line.
        cin.clear();
        std::string ignoreLine;
        std::getline(cin, ignoreLine);
    }

    if (number > 0) {
        positiveCount++;
    }
    else if (number < 0) {
        negativeCount++;
    }
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180