How to fix "no instance of overloaded function "getline" matches the argument list" when using getline with an int

Tommy Beaton

Im currently working on an assignment and am trying to use try catch error handling for checking a user's input is a valid int.

I currently have this:

int inputValidation() {
    int e = 0;
    std::string es;
    bool check = false;
    do {
        try {
            if (!getline(std::cin, e)) {
                throw stringInput;
            }
            else {
                check = true;
            }
        }
        catch (std::exception& er) {
            std::cout << "Error! " << er.what() << std::endl;
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    } while (!check);
    return e;
}

my issue is with the if ((getline(std::cin, e))){} part. I've also tried using std::cin.getline(e, 256)

When calling the function I'm using this loop:

do {
        std::cout << "Please select a month: ";
        selectedMonth = inputValidation();
    } while (selectedMonth < 1 || selectedMonth >(12 - actualMonth));

This simply makes sure they can only input a month from the current month until December.

I'm aware that I can use es instead of e, however that defeats the purpose of the error checking. My only idea coming off of this is to check on a conversion.

For whatever reason I seem to be getting the error "no instance of overloaded function "getline"" and am unsure of where I'm going wrong. If anyone could provide some insight I would be very grateful.

Tommy Beaton

First Version

I managed to fix this by changing it to:

int inputValidation(std::string message) {
    int e = NULL;
    std::string es;
    bool check = false;
    do {
        try {
            std::cout << message;
            getline(std::cin, es);
            if (!atoi(es.c_str())) {
                throw stringInput;

            }
            else {
                e = atoi(es.c_str());
                check = true;
            }
        }
        catch (std::exception& er) {
            std::cout << "Error! " << er.what() << std::endl;
        }
    } while (!check);
    return e;
}
//In another function -->
    do {
        selectedMonth = inputValidation("Please select a month: ");
    } while (selectedMonth < 1 || selectedMonth >(12 - actualMonth));

Working Version

As of the comments I changed this.

(The only difference is this version doesn't include an exception)

bool checkInput(int &input, std::string message) {
    try {
        std::cin >> input;
        if (!std::cin.fail()) {
            return true;
        }
        else {
            throw(message);
        }
    }
    catch (std::string e) {
        std::cout << "Invalid input!" << std::endl;
        std::cout << e << std::endl;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return false;
    }
}

//Elsewhere -->
std::cout << "Please input the lowest number you would like to check" << std::endl;
while (!checkInput(lowestNumber, "Please input the lowest number you would like to check"));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error No instance of overloaded function "getline" matches the argument list

No instance of overloaded function for getline

Using GetLine with ifstream - no instance of "getline" matches the argument list

no instance of "getline" matches the argument list

C++ No instance of overloaded function matches the argument list when calling std::replace()

C++ more than one instance of overloaded function matches the argument list when creating a header file

C++ Getline issues (No instance of overloaded function "getline"

No instance of overloaded function matches the argument list

no instance of overloaded function "std::make_unique" matches the argument list

c++ getline() error, no instance of overload function "getline" matches the argument list

getline() not taking delimiter argument (ERROR: no instane of overloaded function "getline" matches argument list

How to fix an "no instance of constructor matches argument list" error in C++?

Why am I having trouble with getline()? "No instance of overloaded functions matches the argument list" and "data is ambiguous"

Error passing Eigen matrix to a function in C++: "no instance of overloaded function matches the argument list"

More than one instance of overloaded function matches the argument list and I can't find where the error happens

no instance of overloaded function "std::make_unique" matches the argument list, but works with unique_ptr constructor

No instance of overloaded function winrt::Windows::UI::Xaml::Controls::Primitives::SelectorItem::Content matches the argument list

c++ template - More than one instance of overloaded function matches the argument list

c++ struct in map as value - error "no instance of overloaded function matches the argument list"

C++ and SFML, no instance of the overloaded function "sf::RenderWindow.draw()" matches the argument list

No instance of overloaded function when resizing

No instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=Enemy *, _Alloc=std::allocator<Enemy *>]" matches the argument list

How to reduce the temp object when using push_back and getline?

How can I fix a "no instance of function template 'Defer' matches the argument list" error in C++?

Segmentation fault when using getline() in a loop

How and when to use the getline function to perform calculations?

How to input a string from a structure using getline?

How to print string in C++ using getline

ValueError when converting to int lines retrieved via getline.linecache