1
votes

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;
    }
} 
1
Google "C++ numeric_limits" This will take you to cplusplus.com/reference/limits/numeric_limits and you will see that you need to #include <limits>.Matt

1 Answers

7
votes

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;