How to error proof integers from characters?

I have this bit of code..
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
void fDisplayExplanation(){
    cout << "This program asks the user to enter a loop count,\n"
         << "reads the loop count to the user,\n"
         << "then reads the sum of the loop to the user.\n"
         << endl;
}

int fGetSum(){
    int nCount;
    do{
        cout << "Please enter loop count: ";
        cin >> nCount;
        cout << endl;

        if(nCount <= 0){
            cout << "Please enter a positive integer." << endl;
            cout << endl;
        }
    }
    while(nCount <= 0);

    int nSum = 0;
    for(int i = 1; i <= nCount; i++){
        nSum += i;
        cout << "The loop count is " << i << "." << endl;
    }
    return nSum;
}

int main(int nNumberofArgs, char* pszArgs[]){
    fDisplayExplanation();
    int nLoopSum = fGetSum();
    cout << endl;
    cout << "The total sum of the loop is " << nLoopSum << ".\n" << endl;

    system("PAUSE");
    return 0;
}


I have the while loop programmed to loop if an integer below or equal to 0 is entered. I want it to loop if the value entered is anything other than an integer range above or equal to 1 (so alphanetical characters, symbols, etc). Might this be easy enough for me to code an grasp?
closed account (N85iE3v7)
I am not sure if this is what you need, but there are some ANSI C functions in the header ctype.h (in C++ one needs to include cctype ), that might aid you please take a look at

http://www.cplusplus.com/reference/clibrary/cctype/
Last edited on
I see that checks characters, is there something that checks integer input for an alphabetical character and then returns an error? Or better yet, checks for anything other than an integer and returns an error?
closed account (N85iE3v7)
Not sure yet about what you are looking for but

1
2
3

int isdigit ( int c ); // checks if it is a digit decimal representation


Last edited on
Topic archived. No new replies allowed.