我不断收到此错误:无法将 'std::ostream {aka std::basic_ostream<char>}' 左值绑定到 'std::basic_ostream<char>&&'

时间:2021-06-20 16:06:12

标签: c++ string

所以我在这里阅读了很多关于此错误的答案,但我仍然对错误和发生的事情感到非常困惑。所以我有这个代码,我现在有两个函数要求输入 2 个字母和 2 个数字。我得到的错误是在 switch 语句中,它说“std::cout<

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <stdexcept>
#include <limits>
using namespace std;

//overloaded input functions
char input(const std::string& prompt, const std::string& error);
double input(double max_num, double min_num, const string& prompt, const string& error_text);
int main() {
    cout << "Display the function between two items: letters, numbers, points.\n";

    char options {};

    do {
        cout << "Options: l)etter; n)umber; p)oint; q)uit: ";
        cin >> options;

        options = static_cast<char>(std::tolower(static_cast<unsigned char>(options)));

        switch (options) {
            case 'l':
            {
                const string error {"Sorry, out of range. Try again."};
                const auto letter1 {input("Enter first letter (a to z): ", error)};
                std::cout<<letter1;
                const auto letter2 {input("Enter second letter(a to z): ", error)};
                std::cout<<letter2;
            }
            break;

            case 'n':
            {
                const string error_text {"Sorry, out of range. Try again."};
                const auto num1 {input(100, -100, "Enter first number (100 to -100): ", error_text)};
                std::cout<<num1;
                const auto num2 {input(100, -100, "Enter second number (100 to -100): ", error_text)};
                std::cout<<num2;
            }
            break;

            case 'q':
                break;

            default:
                std::cout << "Invalid option\n";
                break;
        }
    } while (options != 'q');
}

char input(const std::string& prompt, const std::string& error_message) {
    char letter {};

    while ((std::cout << prompt) && (!(std::cin >> letter) || !std::isalpha(letter))) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << error_message << '\n';
    }

    return letter;
}

double input(double max_num, double min_num, const string& prompt, const std::string& error_text) {
    double num {};

    do {
        std::cout << prompt;

        if ((std::cin >> num) && (num >= min_num) && (num <= max_num) && std::isspace(std::cin.peek())) {
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            return num;
        }

        std::cout << error_text << '\n';
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    } while (true);
}

0 个答案:

没有答案