[英]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.