0

I have this C++ code, which was compiling normally on VsCode C++ extension. Now I try to run same on CodeBlocks, but i get following error:

C:\Users\Downloads\task.cpp|11|error: 'numeric_limits' was not declared in this scope|
C:\Users\Downloads\task.cpp|11|error: expected primary-expression before '>' token|
C:\Users\Downloads\task.cpp|11|error: no matching function for call to 'max()'|

Can somebody enlighten me, what could be done next with this?

The code is as follows:

#include <iostream>
using namespace std;

int read_number(){
    int x;
    while (true){
        cin >> x;
        if (!cin){
            cout << "Please enter a round number.\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            continue;
        }
        else break;
    }
    return x;
}

1 Answers1

1

The error was occurring because you did not include the header file.

#include <iostream>

#include <limits> // new line added here to add header

using namespace std;

int main(){
    int x;
    while (true){
        cin >> x;
        if (!cin){
            cout << "Please enter a round number.\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            continue;
        }
        else break;
    }
    return x;
}
Kazi
  • 381
  • 2
  • 13
  • 1
    Avoid [```using namespace std```](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – francesco Sep 27 '21 at 07:30