Hello. I'm learning C++ for personal use, and I'm having some trouble.
As you can see, the program rejects the input if it is a letter. However, if I were to type "9s", the check would pass the value of 9 to EvenOdd. I would rather let the user determine if they should change the number. Thank you!Code://Here are includes, the prototype, and using namespace std; int main() { int testnumber = 0; cout<<"Hi! This is the Even or Odd program! Please type a number to determine if it's odd or even!" <<"\nIf you want to exit, please hit 0!\n"; while(!(cin >> testnumber)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<"That is not a number! Please get rid of any decimals, and write a number!\n" <<"If you want to exit the program, please hit 0!\n"; } EvenOdd(testnumber); cout<<"Thank you for using this program! Hit enter to exit."; cin.get(); return 0; } int EvenOdd(int numero) { if(numero == 0) { return 0; } if(numero % 2 == 0) { cout<<numero <<" is an even number!\nPlease enter another number! " <<"If you want to exit, please hit zero!\n"; } else { cout<<numero <<" is an odd number!\nPlease enter another number!\n" <<"If you want to exit, please hit zero!\n"; } while(!(cin >> numero)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<"That is not a number! Please get rid of any decimals, write a new number, and hit enter!\n" <<"If you want to exit the program, please hit 0!\n"; } return EvenOdd(numero); }
-ilikepie2221



LinkBack URL
About LinkBacks


