2016-11-13 78 views
-4

是否有一种简单的方法将char选项从小写q转换为大写Q?do while while循环选择在C++中使用toupper

我已经尝试了toupper的c版本,但我无法让它在C++中工作。我需要输入的所有字符都是大写的;所以,他们链接到主选择的选择。

例如,如果他们键入c它将转向C并且可以访问或使用C链接的函数。

代码至今没有任何变化:

include <iostream> 
#include <stdlib.h> 
#include <string> 
#include "link.h" 

using namespace std; 

int main() 
{ 
    link obr; 
    string n; 
    long int x; 
    char choice; 

    do{ 
    cout << "C: Create/Add\n P: Display\nQ: Quit"; 
    cin >> choice; 

     if(choice == 'C'){ 
       cout << "Name"; 
       cin >> n; 
       cin >> x; 
       obr.push(n,x); 
     } 

    if (choice == 'P'){ 
     obr.display(); 
    } 
} while(choice != 'Q'); 

    return 0; 
} 
+0

发布您的代码,现在它的范围太广。 C函数可以从C++ –

+1

调用'std :: toupper'应该可以工作。你忘了包含相关标题吗?你可以创建一个[mcve],并询问为什么带有'std :: toupper'的代码不能编译:) < - 可以比现在更好地回答,因为最好的答案是'std :: toupper' :) – Rakete1111

+0

I会认为在不使用标准库函数的情况下,将小写或大写转换为家庭作业,或反之亦然,这将成为学习C++的入门课程的一部分。至少它是在我的日子里...... –

回答

1

只要写

#include <cctype> 

//... 

choice = std::toupper((unsigned char)choice); 

提供可变choice具有类型char

您应该确定选择确实包含字母字符而不是控制符号。

1

如果你能保证你的字符集是一个用C的toupper()兼容,那么你可以做到这一点很平凡:

std::string s = "this is a string"; 
std::transform(s.begin(), s.end(), s.begin(), ::toupper);