#include <iostream>
usingnamespace std;
int main ()
{
int num;
cout << "Enter a positive integer:";
cin >> num;
if (num >= 0)
if (isalpha (num))
{
cout << num << " you entered.";
}
else
{
cout << "Thats not a positive number.";
}
}
If you enter something that isn't a number, cin >> num; will fail.
if (isalpha (num))
What are you trying to test here? num is an int, yet you're checking to see if it's a letter? If the user tried to enter a letter, then whatever they entered wouldn't be stored in num, because cin >> num; would have failed.
I'd just keep it simple with a do while loop. I'm kinda newbie but it'd be something to work with. Then with the while at the bottom, do greater than or equal to 0 and if it's anything else, incorrect input.
#include <iostream>
usingnamespace std;
int main ()
{
int num;
cout << "Enter a positive integer:";
cin >> num;
if (num >= 0)
{
// if (isalpha (num))
// {
cout << num << " you entered.";
//
}
else
{
cout << "Thats not a positive number.";
}
}
if you want to block off stupid inputs like characters, use this:
#include <iostream>
usingnamespace std;
int main ()
{
int num;
cout << "Enter a positive integer:";
cin >> num;
while (cin.fail())
{
cin.clear(); //clears cin
cin.ignore(256,'\n'); //ignores the first 256 characters until new line.
cout << "Please make sure it's a number.\n"
<< "Enter a number:";
cin >> num;
}
if (num >= 0)
{
// if (isalpha (num))
// {
cout << num << " you entered is positive.";
//
}
else
{
cout << "Thats not a positive number.";
}
}