How to trap negative integer inputs in C++ program?

A program that asks 10 positive integers and classify them as odd or even positive integer inputs. The algorithm has to trap negative integer inputs. The output will be displayed in two columns. ODD and EVEN.

here's my program code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

 main()
 {
   int countA;
       
   for(countA=0;countA<=10;countA++)
    {
      cin>>countA;
    }
  cout<<"EVEN\tODD\n";
  for(countA=0;countA<=10;countA++)
    {
      if(countA%2==0)
      cout<<" "<<countA;
      else
      cout<<"\t"<<countA<<endl;
    }        
  return 0;
 }


Please help me guys
Last edited on
trap? do you mean catch negatives and notify invalid output? or re-prompt until a positive number is caught
Yes, that is exactly what I mean so how can I catch negatives and notify invalid output? or re-prompt until a positive number is caught... please help me.
Last edited on
First of all, you need an array if you want to capture 10 integers. Right now you're just overwriting the same one again and again. Let's fix that first by changing your declaration for 'countA' to this:

1
2
const int countA_Size = 10;
int countA[countA_Size ];


And your first for loop to this:

1
2
3
4
5
6
7
for(int i = 0; i < countA_Size; ++i)
{
     do
     {
          std::cin >> countA[i];
      }while(countA[i] <= 0);
}


I'll leave fixing the second for loop up to you.
Last edited on
Topic archived. No new replies allowed.