nave@lemmy.zip to Lemmy Shitpost@lemmy.worldEnglish · 2 years agoI wishi.imgur.comexternal-linkmessage-square92linkfedilinkarrow-up1312arrow-down19
arrow-up1303arrow-down1external-linkI wishi.imgur.comnave@lemmy.zip to Lemmy Shitpost@lemmy.worldEnglish · 2 years agomessage-square92linkfedilink
minus-squareGoosLife@lemmy.worldlinkfedilinkarrow-up23·edit-22 years agoThere is absolutely no need to add a check for each individual number, just do this: #include #include int main() { int number = 0; int numberToAdd = 1; int modifier = 1; std::cout << "Is your number [p]ositive or [n]egative? (Default: positive)\n"; if (std::cin.get() == 'n') { modifier *= -1; } std::cin.ignore(std::numeric_limits::max(), '\n'); // Clear the input buffer bool isEven = true; bool running = true; while (running) { std::cout << number << " is " << (isEven ? "even" : "odd") << ".\n"; std::cout << "Continue? [y/n] (Default: yes)\n"; if (std::cin.peek() == 'n') { running = false; } number += numberToAdd * modifier; isEven = !isEven; std::cin.ignore(std::numeric_limits::max(), '\n'); } std::cout << "Your number, " << number << " was " << (isEven ? "even" : "odd") << ".\n"; }```
minus-squareGoosLife@lemmy.worldlinkfedilinkarrow-up7·2 years agoSorry, let me try again. Here is a different attempt that uses modulo to determine if a number is odd or even: #include #include #include #include #include #include #include #include #include #include template bool isEven(const T& number) { std::vector digits; std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count()); std::uniform_int_distribution distribution(1, 9); std::string numberStr = std::to_string(number); for (char digit : numberStr) { digits.push_back(distribution(generator)); } int sum = std::accumulate(digits.begin(), digits.end(), 0); return sum % 2 == 0; }
minus-squarenogrub@lemmy.worldlinkfedilinkarrow-up2·2 years agoi think you forgot an include there also a goto statement would work better
There is absolutely no need to add a check for each individual number, just do this:
I hate this
Sorry, let me try again. Here is a different attempt that uses modulo to determine if a number is odd or even:
i think you forgot an include there also a goto statement would work better