Home > Net >  C error invalid conversion from 'char' to 'const char*'
C error invalid conversion from 'char' to 'const char*'

Time:11-13

#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

CodePudding user response:

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.

CodePudding user response:

What source are you learning C from? This is more like what I would expect from C :

#include <string>
#include <iostream>
#include <unordered_map>

// lookup table
static std::unordered_map<char, char> mapping
{
    {'I','1'}, {'i','1'},
    {'R','2'}, {'r','2'},
    {'E','3'}, {'e','3'},
    {'A','4'}, {'a','4'},
    {'S','5'}, {'s','5'},
    {'G','6'}, {'g','6'},
    {'T','7'}, {'t','7'},
    {'B','8'}, {'b','8'},
    {'O','9'}, {'o','9'},
    {'U','0'}, {'u','0'},
};

int main()
{
    //std::string input;
    //std::cin >> input;

    std::string input{ "TESTcase" };
    for (const char c : input)
    {
        // check if key can be found
        auto it = mapping.find(c);

        if (it == mapping.end())
        {
            // if not cast to upper, std::toupper doesn't return a char
            // so cast it.
            std::cout << static_cast<char>(std::toupper(c));
        }
        else
        {
            // structured binging
            const auto&[key, value] = *it; // *it refers to an key value pair in the map
            std::cout << value;
        }
    }

    return 0;
}

CodePudding user response:

To compare chars, you use == and a character literal, e.g.

if (u == 'O')

.

Also printing a single char with the format specifier for a null terminated string (%s) results in undefined behaviour.

You could just modify the inpur array instead and print everything in one go...

Since you tagged this C , I'll present a C solution:

int main() {
    char S[10007];

    std::scanf("%[^\n]", S); getchar();

    int i = 0;

    // modify S to hold the content we want to print
    for (auto& c : S)
    {
        if (c == '\0')
        {
            // end of string
            break;
        }

        c = std::toupper(c);
        switch (c)
        {
        case 'I':
            c = '1';
            break;
        case 'R':
            c = '2';
            break;
        case 'E':
            c = '3';
            break;
        case 'A':
            c = '4';
            break;
        case 'S':
            c = '5';
            break;
        case 'G':
            c = '6';
            break;
        case 'T':
            c = '7';
            break;
        case 'B':
            c = '8';
            break;
        case 'P':
            c = '9';
            break;
        case 'O':
            c = '0';
            break;
        }
    }

    std::printf("%s\n", S);
}
  • Related