# Removing letter from number problem

• 09-14-2010
ilikepie2221
Removing letter from number problem
Hello. I'm learning C++ for personal use, and I'm having some trouble.

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);
}

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!

-ilikepie2221
• 09-14-2010
Jorl17
Perhaps you should study getline and use that. Then you could search the string that you get for letters, or check if it is only a number in some manner.