while loop problem

hi, all.. I have question about my while loop program,

here is my code

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

int main()
{
 int x;	//declare the variable
 int i = 1,j = 1; //declare the variables

 cout<< "Enter a positive odd number: "; //ask user to input how many lines
 
while (cin>>x && x<=0 && x%2==0)
	{
	 	cout<<"enter a positive odd number. Try again: ";
	}

while (cin>>x && x>0 && x%2!=0)
	{
do the rest of the program


I want the proram to keep check the imput and if the user keep put in negative number or even number, the program will ask the user to put in another number.
however, my program only check it once, and than it stop. any solution to this?
Last edited on
Get rid if the semicolon on line twelve. Also your checking if the input is both negative AND odd, where as you want to show the error message when it is either negative OR odd. Change line twelve to something like
while (cin>>x && (x<=0 || x%2==0))
oh yea, that was my bad, because i was copying from do-while back to while loop.
however, now my program keep checking if i put the wrong number and keep looping, however once i put in the right number, it wont go to the second while loop. any ideas?
Topic archived. No new replies allowed.