Whats wrong with my code?

Aug 11, 2018 at 5:13pm
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.";
  }

}
Aug 11, 2018 at 5:19pm
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 Aug 11, 2018 at 5:21pm
Aug 11, 2018 at 5:23pm
I tried entering a positive number but the output is "Thats not a positive number."
Aug 11, 2018 at 5:25pm
but it really work at first test; input a letter of symbol will output correctly.
Aug 11, 2018 at 5:27pm
could be a bug, huh?
Aug 11, 2018 at 5:45pm
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 Aug 11, 2018 at 5:54pm
Aug 11, 2018 at 5:58pm
so how can I avoid this?
Aug 11, 2018 at 6:04pm
Avoid what? It's working as you programmed it. What are you actually trying to do?
Aug 11, 2018 at 6:57pm
i want whenever the user input something other than numbers, it comes out an error. So no alphabets or symbols etc.
Aug 11, 2018 at 7:09pm
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;
}


Aug 12, 2018 at 6:57pm
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.
Aug 12, 2018 at 6:58pm
'a' is greater than 0.
Aug 13, 2018 at 12:41am
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 Aug 13, 2018 at 12:45am
Topic archived. No new replies allowed.