user3038431
user3038431

Reputation: 113

Error: "numeric_limits" was not declared in this scope

I have this program that won't compile for me and I don't understand why, I get the error "numeric_limits" was not declared in this scope and expected primary expression before "int"

Here is my code:

#include <iostream>
#include<string>
#include<vector>
using namespace std;
using std::endl;
using std::vector;
using std::cout;


int main(){

    string lName;
    int yourAge;

    cout << "Please enter your last name: ";
    cin >> lName;
    cout << " Please enter your age: ";
    cin >> yourAge;
    cin.ignore(numeric_limits<int>::max(), '\n');
    if (!cin || cin.gcount() != 1){
        cout << "Invalid age, numbers only please \n";
        return yourAge;
    }

    vector<string> lastNames;
    lastNames.push_back("Jones");
    lastNames.push_back("Smith");
    lastNames.push_back("Whitney");
    lastNames.push_back("Wang");
    lastNames.push_back(lName);

    for(int i = 0; i < lastNames.size(); i++) {
        cout << "Names before removal: " << lastNames[i] << endl;
    }

    lastNames.erase(lastNames.begin()+2);
    for(int i = 0; i < lastNames.size(); i++) {
        cout << "Names after removal: " << lastNames[i] << endl;
    }
} 

Upvotes: 1

Views: 5592

Answers (1)

Yu Hao
Yu Hao

Reputation: 122433

You didn't include the header that std::numeric is in.

#include <limits>

And I suggest instead of using namespace std;, use all the names you need explicitly:

using std::string;
using std::cin;
using std::endl;
using std::vector;
using std::cout;
using std::numeric_limits;

Upvotes: 7

Related Questions