Whats wrong with my code?

Works at first, now it outputs incorrectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace 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.
Last edited on
I tried entering a positive number but the output is "Thats not a positive number."
but it really work at first test; input a letter of symbol will output correctly.
could be a bug, huh?
isalpha would typically return true if the input value is anything from 65 to 90, and also from 97 to 122.

isalpha(64) returns false.
isalpha(65) returns true.

If you enter 64, you'll get "Thats not a positive number."
If you enter 65, you'll get "65 you entered".

If you enter the letter 'a', that will be interpreted as the number 97


Last edited on
so how can I avoid this?
Avoid what? It's working as you programmed it. What are you actually trying to do?
i want whenever the user input something other than numbers, it comes out an error. So no alphabets or symbols etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
  string userInput;
  cin >> userInput;

  for ( auto& element : userInput)
  {
     if ( !isdigit(element))
    {
        cout << "Not just numbers";
        return;
    }
  }
cout << "Is just numbers";
return;
}


so no alphabets or symbols

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.
'a' is greater than 0.
your {} for the if statement is in the wrong place. try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace 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.";
  }

}
Last edited on
Topic archived. No new replies allowed.