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



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

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

#include <iostream>
#include <string> 
using namespace std;
void swapCase (const string& s)
{
string str;
str.assign(s);
for(int i=0;str[i]!='';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?的答案是:U不能为常量变量赋值,U只能初始化常量变量。在你的情况下,你可以做以下事情(只显示可能性,不建议这样做(:

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

你的这个问题让我认为你认为你不能将非常量变量传递到swapCase中。这是错误的:u可以将任何字符串传递给swapCase。函数参数中的const修饰符表示u不会在函数内更改此变量。所以你可以简单地做:

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]!='' 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,因为它只会在控制台中引起抖动,而不会在文件中引起抖动(这是一个粗略的近似值(,请参阅下面的代码:

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

相关内容

最新更新