even character "a" detected as "EVEN"

Oct 14, 2019 at 2:56pm
its good, numbers detected well if its even or odd.
but when i input letters or other symbols it shows "even" help me! SOS! Asap TNX XD!

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
  #include <iostream>

using namespace std;

int main()
{
    int num;
    cout << "Input:" << endl;
cin>>num;


    switch(num%2)
    {
    case 0:
        cout<<"even";
        break;
    case 1:
        cout<<"oDD";
        break;
        default:
            break;

    }

    return 0;
}
Last edited on Oct 14, 2019 at 2:58pm
Oct 14, 2019 at 3:07pm
but when i input letters or other symbols it shows "even" help me!

Okay, sounds normal.

Did you try "printing" the value of num after the cin?

You do realize that the insertion operator>> will not accept a "letter" for a numeric variable, right?

Do you know what happens to the variable if an insertion operation fails?

Do you know what happens to the stream if an insertion operator fails?


Oct 14, 2019 at 7:13pm
i think it convert the letter into something numeric, So what must be the solution?

I though default is enough to hold for the values thats not required
Oct 14, 2019 at 7:55pm
i think it convert the letter into something numeric

Sort of, when the conversion failed it default initialized the int and set the stream into an error state. Which means it was initialized to zero, which is even.

So what must be the solution

Easy answer, don't try to insert an alpha character into a numeric value.

Harder answer check the state of the stream, if it is in an error state take the appropriate actions. Ie: clear the stream error flag, clear the input buffer, ask the user for a "proper" value.

Oct 15, 2019 at 1:42am
One way to make sure the user can only enter positive integer numbers:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <limits>

int main()
{
   // unsigned to prevent evaluating negative numbers
   unsigned           response { };

   std::string        input    { };

   // arbitrary limit for the largest number user can input
   unsigned constexpr nmax     { (std::numeric_limits<unsigned>::max() / 100) };

   while (response < 1 || response > nmax)
   {
      std::cout << "Input: ";
      std::getline(std::cin, input);
      std::cout << '\n';

      // can the input string be evaluated as an unsigned long integer?
      try
      {
         response = std::stoul(input);
      }

      catch (...)
      {
         std::cout << "** INVALID CHOICE! **\n\n";
         continue;
      }

      if (response < 1 || response > nmax)
      {
         std::cout << "** INVALID CHOICE! **\n\n";
         continue;
      }

      switch (response % 2)
      {
      case 0:
         std::cout << "even\n";
         break;

      case 1:
         std::cout << "odd\n";
         break;

      default:
         std::cout << "** INVALID CHOICE! **\n\n";
         continue;
      }
   }
}

Input: -1

** INVALID CHOICE! **

Input: a

** INVALID CHOICE! **

Input: 14

even

The input stream is never put into an error state. A non-numeric value is caught as an input error.
Oct 15, 2019 at 2:29am
Since C++14* input failure assigns a default value to the target object.**

So if you try to input 'a' as an integer, the input operation fails, zero is assigned to num, and your test takes zero as even.


I think. Not gonna look it up.

** Which, IMNSHO, is a really, really dumb thing to do...
Topic archived. No new replies allowed.