0

In C++ I want to take input 1 for even number output and input 2 for odd number output. How I limit input to user.

I made a program, which give all even numbers from (1-100) after giving output any number. I want to limit input to 1. How can I do this.

31st May 2017, 5:23 PM
Umair
Umair - avatar
3 Antworten
+ 6
See the project I made here. It limits the users choices to what is available in the store https://code.sololearn.com/cVsqivdibVH0/#cpp Example int numberOfAction = 4; int userInput; if (!(std::cin >> userInput)) { std::cout << "Not a number.\n\n"; std::cin.clear(); std::cin.ignore(9999, '\n'); } else if (userInput - 1 < 0 || userInput > numberOfActions) { std::cout << "That was not between 0 - " << numberOfActions << "\n\n"; std::cin.clear(); std::cin.ignore(9999, '\n'); } else if (userInput > 0 || userInput < numberOfActions) { std::cin.clear(); std::cin.ignore(9999, '\n'); } }
31st May 2017, 5:34 PM
jay
jay - avatar
+ 4
@parker; mmhmm that was on purpose as the program I took it from had 0 for exit, so it (0) had its own if clause which i deleted as the example was already verbose. But yes. The above example does disclude 0. Good Spot!
1st Jun 2017, 1:47 AM
jay
jay - avatar
+ 1
@jay, there's a small error in that example, the first "else if" statement will execute if the input is 0, making it necessary to be between 1-4, and not 0-4 as said in the output, and the last else if statement will execute no matter what, because of the "||".
1st Jun 2017, 12:54 AM
parker persinger
parker persinger - avatar