函数读取最大和min int值,并用文本字符串返回

Function reads max and min int value, returns it - with text string

本文关键字:文本 返回 字符串 int 读取 min 函数      更新时间:2023-10-16

我得到了一个返回数字的函数。但是,在使用该功能时,似乎我在使用该功能时做错了什么,在我没有像指针一样使用它 - 但是我该怎么做?

'read':两个超载都不能转换所有参数类型是我遇到的错误。

这是代码:

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;
}

如果您更详细地查看错误消息,您可能会看到类似" const char*"转换为" cont char*"的内容。

字符串文字具有" const char*"类型,因此您的读取方法必须为:

int read(const char* t, int min, int max) {