繁体   English   中英

在 C++ 中使用 get 和 set 方法从用户那里获取输入?

[英]Take the Input from the user using get and set method in c++?

我正在使用 get 和 set 方法以及 C++。 我想从用户那里获取输入并打印该字符串大写

例如:

当用户输入名称时: alex然后 o/p 是ALEX

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

class abc
{
    private: 
    string name_;

     //take the input from the user
     //cout << "Enter the Name: ";
     //cin >> name_;

    public: 

      string getname()
      {
        return name_;
      }

      string setname(string name)
      {
          name_=name;
      }
};

int main()
{
  abc a;
  string name_=a.getname();
  cout << name_;

  _name(toupper(name_));

  return 0;
}

我知道一个函数是 isupper(),我参考这个链接: http : //www.cplusplus.com/reference/cctype/toupper/

我试图从用户那里获取输入并以大写形式打印该字符串,但出现错误:

main.cpp: In function ‘int main()’:
main.cpp:44:22: error: no matching function for call to ‘toupper(std::string&)’
   _name(toupper(name_));
                      ^

我想做什么: https : //onlinegdb.com/SyZnXIkUI

您的问题是 toupper() 没有使用 std::string,它只需要一个字符。 因此,要将整个字符串转换为大写,您需要执行以下操作:

std::string text;

std::cin >> text;

for(auto &c : text)
{
    c = toupper(c);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM