C ++代码中的getline错误将用户输入的字符串的大写转换为小写,反之亦然

getline error in C++ code to swap uppercase to lowercase and vice-versa of a user inputted string

我正在学习 C++,我遇到了这个我似乎无法修复的错误。

代码应该从用户那里获取一个字符串,然后将大写字符交换为小写字符,将小写字符交换为大写字符:

#include <iostream>
#include <string> 
using namespace std;

void swapCase (const string& s)
{
    string str;
    str.assign(s);
    for(int i=0;str[i]!='[=10=]';i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')    //checking for uppercase characters
        {
            str[i] = tolower(str[i]);
        }         //converting uppercase to lowercase
        else if (str[i] >= 'a' && str[i] <= 'z')   //checking for lowercase characters
        {
            str[i] = toupper(str[i]);        //converting lowercase to uppercase  
        }
    }
    cout << str << endl; // output
}

int main()
{
    const string str;
    cout << "Enter the string "; //prompt user
    getline(cin, str); // input
    swapCase(str); //send string to function
    return 0;
}
main.cpp:23:3: error: no matching function for call to 'getline'
  getline(cin, &str);

让我总结一下评论: how would I assign a variable to str if I want to keep it as const?答案是:你不能给常量变量赋值,你只能初始化常量变量。 在您的情况下,您可以执行以下操作(仅显示不建议这样做的可能性):

    string temporary_str;
    getline(cin, temporary_str); // input
    const string str = temporary_str; // not assigning but initializing
    // cant do after initialization: str = tremporary_str; // it is assignation cant be done with const on left side
    swapCase(str); //send string to function

你的这个问题让我觉得你认为你不能将 non-const 变量传递给 swapCase。这是错误的:你可以将任何字符串传递给 swapCase。函数参数中的 const 修饰符意味着您不会更改此变量 inside 函数。所以你可以简单地做:

    string str;
    cout << "Enter the string "; //prompt user
    getline(cin, str); // input
    swapCase(str); //send string to function

其他一些建议(在代码注释中): 像这样使用 std::isupperstd::islower

#include <iostream>
#include <string> 
using namespace std;

void swapCase (const string& s)
{
    string str = s; // can be done without assign
    for(int i=0; i < str.size(); i++) // not sure that str[i]!='[=12=]' is good idea for c++
    {
        if (isupper(str[i]))    //checking for uppercase characters
        {
            str[i] = tolower(str[i]); //converting uppercase to lowercase
        }
        else if (islower(str[i]))   //checking for lowercase characters
        {
            str[i] = toupper(str[i]);        //converting lowercase to uppercase  
        }
    }
    cout << str << endl; // output
}

int main()
{
    string str;
    cout << "Enter the string "; //prompt user
    cout << std::flush; // I recommend it to be exactly displayed on the screen
    getline(cin, str); // input
    swapCase(str); //send string to function
    return 0;
}

编辑: 关于冲洗的一些信息: 当你写 cout << something; 它实际上并没有立即写它,它说保存它来写并且有一天它写出来(如果程序以 0 代码结束它写它承诺的),实际上刷新会不时自动发生但是要强制写入内部缓冲区中的内容,请使用 std::flashstd::endl 因为它包含 std::flush 内部,刷新它的另一面会减慢程序,所以如果你知道你会写一些东西和你实际上不需要立即打印不要使用 std::endl 使用 \n 因为它只会在控制台而不是在文件中导致 flaush(这是一个粗略的近似值),请参见下面的代码:

#include <iostream>
#include <fstream>

int main() {
    std::cout << "This will be printed because endl have flush inside it" << std::endl;
    std::cout << "This will be printed because flush after it" << std::flush;
    std::cout << "This will be printed in console because in console \n means flush  \n";
    std::ofstream example_file("example.txt");
    example_file << "This will be printed in file because endl have flush inside it" << std::endl;
    example_file << "This won't be printed in file even with \n because writing in file \n doesn't mean flash \n";
    std::cout << "This won't be printed";
    _Exit(1); // imitates something bad happend
}