0

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.

1
  • You're going to quickly exceed your bounds here. Check what you're doing w/ x. Commented Jul 1, 2014 at 20:52

3 Answers 3

4

!< 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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have to change the entire program.
1

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++;
}

1 Comment

if (input > 0) should probably be removed. You have already done the validity check in the while loop. With that said, if 0 did get through, with the current if, you increment the count but do not store the value, which is probably not the desired behaviour.
0

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++;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.