-4

I got a function that returns a number. However it seems like there's something I do wrong when using the function, before I didn't use it like a pointer - but how do I do that?

'read': none of the 2 overloads could convert all argument types is the error I'm getting.

Here's the code:

int nr = read("This is a test", 0000, 9999);
cout << nr;

int read(char* t, int min, int max) {
    int number;
    do {
        cout << '\t' << t << " (" << min << '-' << max << "):  ";
        cin >> number;  cin.ignore();
    } while (number < min || number > max);
    return number;
}

1 Answers1

2

If you look at the error message in more detail you will probably see something like "cannot convert argument 1 from "const char*" to "char*".

String literals have the type "const char*" so your read method needs to be:

int read(const char* t, int min, int max) {
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    String literals have the type `const char[n]` where `n` is the size. It only decays to a pointer like any other array. Otherwise the answer is correct. – DeiDei Feb 22 '18 at 16:28
  • Error message is "'read' none of the 2 overloadws could convert all t he argument types". Still getting that even though I changed to const – Ultralight Feb 22 '18 at 16:31
  • If you post the full message or tell us the compiler we might be able to help – Alan Birtles Feb 22 '18 at 16:33
  • Pretty much given the whole code in the text above, but here: `#include using namespace std; int read(const char* t, int min, int max); int main() { int nr = read("This is a test", 0000, 9999); cout << nr; return 0; } int read(const char* t, int min, int max) { int number; do { cout << '\t' << t << " (" << min << '-' << max << "): "; cin >> number; cin.ignore(); } while (number < min || number > max); return number; }` – Ultralight Feb 22 '18 at 16:49
  • that code compiles for me... I was asking for the full error message and the compiler you are using though. Most compilers would give more details than that – Alan Birtles Feb 22 '18 at 16:56
  • @Ultralight get rid of `using namespace std;`. It only [causes problems](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – NathanOliver Feb 22 '18 at 17:02