Help with limiting my cin to just integers

Hello All,

This is my first post here, and I am about 3 weeks into learning C++. I wrote this program and it works great but I want to limit it to only except integers. Ideally, I'd like to know if there is a way to do this withing an if statement. If say, the letter b is entered, I'd like it to say something like "invalid entry." can someone help?

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

using namespace std;

int main()
{
    double leng1,
           leng2,
           width1,
           width2,
           rec1,
           rec2;

    cout << "let's compare the area of two rectangles.\n"
         << "First enter length and the width of Rectangle One in feet\n"
         << "Length: ";
    cin >> leng1;
    cout << "Width: ";
    cin >> width1;

    cout << "\nGreat, now the same for Rectangle Two.\n"
         << "Length: ";
    cin >> leng2;
    cout << "Width: ";
    cin >> width2;

    rec1 = (leng1 * width1);
    rec2 = (leng2 * width2);

    cout << fixed << showpoint << setprecision(3);

    if (rec1 > rec2)
    {
        cout << "\nRectangle One has an area of " << rec1 << " feet, \n"
        << "which is larger then Rectangle Two's area of " << rec2 << " feet.";
    }
    else if (rec1 < rec2)
    {
        cout << "\nRectangle Two has an area of " << rec2 << " feet, \n"
        << "which is larger then Rectangle One's area of " << rec1 << " feet.";
    }
    else if (rec1 == rec2)
        cout << "\nRectangles One and Two are equal";

    else
        cout << "Invalid entry";


    return 0;
}
Hope this helps.

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

using namespace std;

int getInt();

int main()
{
    int test;
    cout<<"Enter an integer only : ";

    test = getInt();
}
int getInt()
{
    string test;
    getline(cin,test);

    int num;

    istringstream ss(test);
    ss>>num;

    if(num != 0)
        return num;
    else
        throw invalid_argument("Enter an integer!!");
}
Using exceptions for this is not only overkill, it's downright wrong.

The common way to do this is to read it into a string and then use a stringstream to try to read it into an integer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int request_int(std::string const &prompt)
{
    std::string input;
    while((std::cout << prompt << ": ") && (std::cin >> input))
    {
        std::istringstream iss (input);
        int result = 0;
        if(iss >> result)
        {
            return result;
        }
        else
        {
            std::cout << "Invalid input, try again." << std::endl;
        }
    }
    return 0;
}
In the event that there is no more remaining input the function exits the while loop and returns 0. You can easily expand the function by e.g. making it a template and/or adding parameters that limit the range of accepted values.
Last edited on
Topic archived. No new replies allowed.