user3787378
user3787378

Reputation: 9

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.

So how to catch negatives and notify invalid output? or re-prompt until a positive number is caught?

here's my program code...

int x=10;
int a[x];

cout<<"Input :";
cin>>a[x];

while(a[x]!<0) /*[Error] expected ')' before '!' token*/
{              /*[Error] expected primary-expression before '<' token*/
if(a[x]<0)     /*[Error] expected ';' before ')' token*/
 {             /*[Error] expected '}' at end of input*/
  cout<<"The input is negative try again";
  cout<<"Input:";
  cin>>a[x];
 }
else
 {
  a[x++];
 }
}
cout<<"ODD\tEVEN";

for(int y=0; y<=10; y++)
 {
  if(y%2==0)/*It separates the ODD and EVEN*/
   {
    cout<<" "<<a[y];
   }
  else
   {
    cout<<"\t"<<a[y]<<endl;
   }
  }
return 0;
}

What should be the right code for this. I've been debugging this since yesterday.

Upvotes: 0

Views: 3344

Answers (3)

abdul
abdul

Reputation: 1582

Your code is not organized well, even if you manage to fix the errors, it won't produce the result you are seeking for, I have re-written the codes that will check for negative numbers and let user input up to 10 numbers, then split into odd and even groups. Here's what I came up with:

int input = 0;
int itr = 0; //iterator or count looping cycles
int arrayInt[10];
//read user input
while (itr < 10) //iterate while loop runs 10 times (0 - 9) = 10
{
   do
   {
       std::cout << "Enter a number" << itr+1 << ": ";
       std::cin >> input; //read user input
   } while (input <= 0);

   arrayInt[itr] = input; //store input in the array

   itr++; //iterate loop
}

//printing odd & even numbers from the array
itr = 0; //reset iterator

std::cout << "---------------------" <<endl;
std::cout << "Even\t\tOdd" <<endl;
cout << "---------------------" <<endl;
while (itr < 10)
{
    if (arrayInt[itr] % 2 == 0) //check if even
    {
        std::cout << arrayInt[itr] <<endl; //print on left
    } //check odd
    else
    {
        std::cout << "\t\t" << arrayInt[itr] <<endl; //print on right
    }
    itr++;
}

Upvotes: 1

Straw1239
Straw1239

Reputation: 689

The first thing I see is while(a[x]!<0), which is invalid syntax. Are you saying while a[x] is not less than zero? That would be a[x] >= 0. It would make more sense to check if it is less than zero and then reprompt the user. If the while loop finds it is greater than or equal to zero, the first if statement in it can't ever trigger. There are other bugsand anomalies in your program also. For example, why use a[x++];? This just increments x and discards a[x], so just use x++;

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 224864

!< is not a C++ operator. You're probably looking for:

while (a[x] >= 0)

Your program has several other bugs too; you might want to try stepping through with a debugger once you get it compiling.

Upvotes: 4

Related Questions