cstringctypecstdio

C++ error invalid conversion from 'char' to 'const char*'


#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(){
    
    char S[10007];

    scanf("%[^\n]", S); getchar();
    
    int i = 0;
    char u;
    while(S[i]){
        u = toupper(S[i]);
        if(strcmp(u, "I") == 0){
            u = '1';
        } 
        else if(strcmp(u, "R") == 0){
            u = '2';
        }
        else if(strcmp(u, "E") == 0){
            u = '3';
        }
        else if(strcmp(u, "A") == 0){
            u = '4';
        }
        else if(strcmp(u, "S") == 0){
            u = '5';
        } 
        else if(strcmp(u, "G") == 0){
            u = '6';
        } 
        else if(strcmp(u, "T") == 0){
            u = '7';
        } 
        else if(strcmp(u, "B") == 0){
            u = '8';
        } 
        else if(strcmp(u, "P") == 0){
            u = '9';
        } 
        else if(strcmp(u, "O") == 0){
            u = '0';
        } 
        printf("%s", u);
        i++;
    }
    

    return 0;
}

I got a case where i need to make an inputted string uppercase then change some of the uppercase alphabet to the following number, (example input: im waterswell, the otuput: 1M W4T325W33L) so i created the program but it returns to following error: invalid conversion from 'char' to 'const char*' [-fpermissive]. Can anyone help me? thank you


Solution

  • strcmp is used to compare strings, not single characters. Use simply if (u == 'I') and use that everywhere you have strcmp (notice the quote change - we want a character, not a string literal).

    Also, printf("%s", u); is wrong, you need %c to print a char.