isupper(), islower(), toupper(), tolower() 函数在 C++ 中不起作用
the isupper(), islower(), toupper(), tolower() functions not working in c++
我有一个这样的代码片段:
char choice;
do
{
cout << "A. Option 1" << endl;
cout << "B. Option 1" << endl;
cout << "C. Option 1" << endl;
cout << "D. Option 1" << endl;
cout << "Option: ";
cin >> choice;
if(islower(choice) == 0){ toupper(choice); } // for converting Lower alphabets to Upper alphabets so as to provide flexibility to the user
}while((choice != 'A') && (choice != 'B') && (choice != 'C') && (choice != 'D'));
但它不会将低字母转换为高字母......我不知道为什么。。。我正在使用的操作系统是 Windows 7,编译器是 Visual C++(请注意,我已经在其他编译器中测试了此代码,但同样的问题)......
你应该使用返回值,toupper
按值(而不是引用)获取字符并返回大写结果:
choice = toupper(choice);
^^^^^^^^
此外,条件应反转:
if (islower(choice)) // not: if(islower(choice) == 0)
使用此代码,toupper
本身检查字符是否为小写:
cin >> choice;
choice = toupper(choice);
这行代码
if(islower(choice) == 0){ toupper(choice); }
应该重写如下,
if(islower(choice)){ choice = toupper(choice); }
函数,
int toupper ( int c );
返回值大写等效于 c(如果存在此类值),否则为 c(未更改)。该值作为 int 值返回,该值可以隐式转换为 char。
托上
islower
和isupper
告诉字符是大写还是小写。
toupper
或tolower
不会转换。它接受int
参数并返回一个转换字符的int
。
要转换,请使用以下命令:
choice = toupper(choice);
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用