简体   繁体   中英

How to convert lower case string to uppercase string in C++

Hi I want to create a program where I will input different names and I will output it in UPPERCASE FORM. However there's a bug in my code, can you help me to figure it out?

It said "[Error] no match for 'operator>=' (operand types are 'std::string {aka std::basic_string}' and 'int')"

    int times;  
    string name [1000];
    int i = 1;
    int hold = 0;
    int j ;
    int cont;
    
    while( i != 0){
    cout<<"Enter Name "<<endl;
    cin>>name[hold];
    
    times = hold;
    for ( j = 0 ; j <= strlen(name) ; j++){
        
        if (name[j] >= 97 && name[j] <= 122){
            name[j] = name[j] -32;
        }
    }

    
    
    cout<<"\n[1] for Add"<<endl;
    cout<<"[2] for Stop"<<endl;
    cin>>cont;
    
    
    if ( cont == 1){    
        hold++;
        i = 1;
    }else{
        i = 0;
    }
    }
        
        
        for ( i = 0 ; i <= times ; i++){
            
            cout<<name[i]<<"\n";
        }
        
        

You should pass a const char* to the strlen() function. Refer: https://www.programiz.com/cpp-programming/library-function/cstring/strlen

But the 'name' is an string type array. Therefore the compiler returns an error.

Also the data type of name[j] is std::string. Therefore you cannot compare name[j] with an integer.

If you need to convert a string to uppercase string, please use the below code block (str is a string variable).

std::transform(uppers.begin(), uppers.end(), uppers.begin(), [](unsigned char c){ return std::toupper(c); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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