Exercise problem

I have the following exercise:
Write a program to use a conditional operator to find the elements in a vector<int> that have odd values and double the values of each suck element


This is what I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
#include <vector>
#include <string>
#include <iterator>
using std::cout; using std::endl; using std::cin;
using std::vector; using std::string;
using std::begin; using std::end;

int main()
{
    vector<int> 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; // <-
*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);
Thanks guys
Topic archived. No new replies allowed.