So I am trying to do an assignment for my class but for some reason I cant get the output to match with what I need. I have to make a program that asks for 6 integers and display the smallest even and odd. There will be 3 even entered and 3 odd but the order is unknown and we can't use loops or arrays. This sample is just what i have so far but I can't seem to understand why a and b won't work.
int a, b, c, d, e, f, smallEven=0, smallOdd=0;
cout<<"Enter a number:\n";
cin>>a;
if (a%2==0)
smallEven=a;
else if (a%2==1)
smallOdd=a;
cout<<"Enter a number:\n";
cin>>b;
if (b%2==0)
if (smallEven=0)
smallEven=b;
else if (b<smallEven)
smallEven=b;
else if (b%2==1)
if (smallOdd=0)
smallOdd=b;
else if (b<smallOdd)
smallOdd=b;
cout<<smallEven;
cout<<smallOdd;
The reason why this isn't doing what you're expecting is that you're using an assignment operator (=) instead of the comparison operator for equality (==) in two places:
if (b%2==0)
if (smallEven=0) // should be == here
smallEven=b;
else if (b<smallEven)
smallEven=b;
else if (b%2==1)
if (smallOdd=0) // should be == here
smallOdd=b;
else if (b<smallOdd)
smallOdd=b;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.