Comment on I wish

GoosLife@lemmy.world ⁨1⁩ ⁨year⁩ ago

There 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";

}`

source
Sort:hotnewtop