### Exercise problem

closed account (EwCjE3v7)
I have the following exercise:
 Write a program to use a conditional operator to find the elements in a vector that have odd values and double the values of each suck element

This is what I have
 ``12345678910111213141516171819`` `````` #include #include #include #include using std::cout; using std::endl; using std::cin; using std::vector; using std::string; using std::begin; using std::end; int main() { vector v = {1,2,3,4,5,6}; for (auto itbeg = v.begin(); itbeg != v.end(); ++itbeg) { *itbeg % 2 = 1 ? *itbeg += *itbeg : ; // <- cout << *itbeg << endl; } return 0; } ``````

I do not know what to place on the other side of the ':'

And I do not know if I`m even doing it right.
How would you answer this?
You can just put a number since you dont need the second case ,and watch out `*itbeg % 2 = 1` this is not a comparaison

put this one instead
`*itbeg % 2 == 1 ? *itbeg += *itbeg : 2; // <- `
closed account (EwCjE3v7)
`*itbeg % 2 == 1 ? *itbeg += *itbeg : 2; // <- `

Thank you, but what exactly does the 2 there do, As it seem to do nothing
Thank you the = and == is to evil, I could also do this
`cout << ((*itbeg % 2 == 1) ? *itbeg += *itbeg : *itbeg) << endl; // <- `
Last edited on
I said you can put any number ,in your case you dont need a second case check

the Ternary Operators "?" evaluate a true or false result from the condition
condition ? true result : false result
you only need one : square the odd number you have
`cout << ((*itbeg % 2 == 1) ? *itbeg += *itbeg : *itbeg) << endl; ` I would go with this line
`*itbeg = (*itbeg % 2 == 1 ? *itbeg + *itbeg : *itbeg);`
closed account (EwCjE3v7)
Thanks guys
Topic archived. No new replies allowed.