How do I check that an input is of integer value?

I'm trying to write a code that will calculate leap years. I have the fundamental code written and it works fine, but I'm trying to improve my code so that when alphabetical value's are inputted it won't crash my program.
1
2
3
4
5
6
7
8
9
10
11
12
    while ((x!='0')||(x=='1'))
    {
        int leap;
        cin >> leap;
        if (leap !=(int))
            {
                while (leap !=int)
                {
                    cout << "The value you have entered is incorrect.  Please try again\n";
                    cin >> leap;
                }
            }


can anyone help me test the datatype of the input?
1
2
3
4
5
if(cin >> leap) {
    //the read was successful
} else {
    //...
}
the above code will work only if the first character entered is not an integer char.
if the starting characters will be integer characters those will we accepted by the code and gets stored in the variable.



sooo is there any way to check every character of the input?
@ms4800: every char is an integer.

@mongie52: if you want fool proof input checking, read the numbers as chars and check them with isdigit().
@filipe: yea but wouldn't that only work if the input is a single character..? its an array of numeric characters.. or check every character in the array?
Not necessarily:

1
2
3
4
5
6
char ch;
int n = 0;
while(cin >> ch && isdigit(ch)) {
    n *= 10;
    n += ch - '0';
};
I'm sorry if i'm slow at picking this up but i'm kind of new and i don't understand what that means. i've been trying to play around and figure out the isdigit function, i kind of understand it, but i still believe it to be true that it only checks one number. I will put out my code and if anyone can give me specific example regarding my problem i will understand better.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void intro (string);
void outro (string);
void calcleap(int);

int main()
{
    string name;

    cout << "Please enter your user name:";
    cin >> name;

    intro (name);

    char x=1;
    while ((x!='0')||(x=='1'))
    {
        char leap;
        if (cin >> leap && !isdigit(leap))  //here I would like the variable leap to be checked for up to 4 characters to see if they are all of numeric value... is there any way?
            {
                while (!(isdigit(leap)))
                {
                cout << "You have entered incorrectly, please enter again:";
                cin >> leap;
                }
            }



        calcleap(leap);

        cout << "Would you like to run the program again, " << name << "?(1 for yes, 0 for no)\n";

        cin >> x;

        if (x=='1')
            cout << "You have chosen to enter and test another year.\nPlease enter in a 4 digit number:";
        else
            cout << "You have chosen to quit the program.\n";
    }
    outro (name);

    return 0;
}

//Function Definition
void intro (string name)
{
    cout << "Welcome, " << name << ".\n\nThis program is designed to receive an input value(a whole number year) from the user, and decipher whether or not it is a leap year.";
    cout << "\nRemember, a leap year comes ever 4 years, and a century year (every 100 years) is only considered a leap year every 400 years.";
    cout << "\n\nIf the year you enter is not a leap year, this program will calculate the closest leap year before and after the year entered.\n\nPlease enjoy the program, and use it as long as you'd like!";
    cout << "\n\nPlease enter in a whole number year you would like to have calculated:";
}

void calcleap(int leap)
{
    int a=leap-1, b=leap-2, c=leap-3, d=leap+1, e=leap+2, f=leap+3, g=leap-4, h=leap+4;
    {

    if ((leap%4)==0)
        {
            if ((leap%100==0)&&(leap%400==0))
                cout << "Coincidentally, the year you have entered is a leap year and also a century year.\n";
            else if ((leap%100==0)&&(leap%400!=0))
            {
                cout << "The year you have entered is a century year, but is not a leap year.\n\n";
                cout << "The leap year prior to and after the year you entered is:";
                cout << g << " and " << h << ", respectively.\n\n";
            }
            else
                cout << "The year you have entered is a leap year.\n\n";
        }
    else if (leap%4==1)
    {
        if (a % 100 == 0)
            a=leap-5;
        if (f % 100 == 0)
            f=leap+7;
        cout << "Sorry, though, the year you have entered is not a leap year.\n\nThe leap year prior to and after the year you entered is:";
        cout << a << " and " << f << ", respectively.\n\n";
    }
    else if (leap%4==2)
        {
            if (b % 100 == 0)
                b = leap - 6;
            if (e % 100 == 0)
                e = leap +6;
            cout << "Sorry, though, the year you have entered is not a leap year.\n\nThe leap year prior to and after the year you entered is:";
            cout << b << " and " << e << ", respectively.\n\n";
        }
    else if (leap%4==3)
    {
        if (c % 100 == 0)
            c = leap - 7;
        if (d % 100 == 0)
            d = leap +5;
        cout << "Sorry, though, the year you have entered is not a leap year.\n\nThe leap year prior to and after the year you entered is:";
        cout << c << " and " << d << ", respectively.\n\n";
    }
    }
}

void outro (string name)
{
    cout << "Please return some time to test leap years!\nFor now, have a great day, " << name << "!\n";
}
hmm... slow... lets go through the logic of what you asked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// first add a counter variable.
int nCount = 0;
// make a loop for counter 
While (nCount < 5)
{
       char leap;
       if (cin >> leap && !isdigit(leap))  
       //here I would like the variable leap to be checked for up to 4           
       //characters to see if they are all of numeric value... is there any way?
       {
             while (!(isdigit(leap)))
             {
                    cout << "You have entered incorrectly, please enter again:";
                    cin >> leap;
              }
        }
       ++nCount; // increment the count.
} // bottom of the loop 


I don't mean to answer your question that way but that is one way to implement what you ask. I don't know if it is the easiest to answer.

If your logic flows as I read it you already do the rest of the logic for the test. The rest is the accumulators for the leap year that you read in.
Last edited on
Topic archived. No new replies allowed.