简体   繁体   中英

error: invalid conversion from 'char' to 'const char*'

I'm trying to convert all the words into capital letters. Here's the header:

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

using namespace std;

int Mayusculas(char texto)
{
    int liCount;
    for(liCount=0;liCount<strlen(texto);liCount++)
    {
        texto[liCount]=toupper(texto[liCount]);
    }
}

Here is the definition in main

char Cadena[100];

and here is where I am using it

case 1:
    Mayusculas(Cadena);
    cout<<Cadena<<endl;

The error message is

error: invalid conversion from 'char' to 'const char*'

TL;DR:

Details

Since we're primarily English-speaking, I'll note that it appears that Mayusculas indicates capital letters, and cadena is a series or chain - in this case a C-style string.

As Presented - C-style Strings

  1. int Mayusculas(char texto) should be int Mayusculas(char *texto)

It needs to be a char * since you are working with a C-style string, and not a single character. Otherwise you have nothing to iterate through.

  1. toupper() returns an int , so you should cast, ie change

texto[liCount]=toupper(texto[liCount]);

to

texto[liCount] = (char)toupper(texto[liCount]);

  1. The function signature says you're returning an int , but you don't actually return anything. So either change it to return void, or return something. ( liCount , maybe?)

Alternative: std::string

But you tagged this question as C++, so why not use std::string instead of C-style strings? They're safer, and easier to work with.

Quoting Pierre from Convert a String In C++ To Upper Case :

#include <algorithm>
#include <string>

std::string str = "Hello World";
std::transform(str.begin(), str.end(),str.begin(), ::toupper);

Or if you still want it in your own function,

#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

string Mayusculas(std::string str)
{
    transform(str.begin(), str.end(), str.begin(), ::toupper);
    return str;
}

int main()
{
    string Cadena = "Hola al Mundo";
    cout << Mayusculas(Cadena) << endl;
    return 0;
}

That way returns the result as a string. But if you want to modify it in place like your original, you can do this instead. See it work at Ideone .

void Mayusculas(std::string & str) // reference parameter
{
    transform(str.begin(), str.end(), str.begin(), ::toupper);
}

int main()
{
    string Cadena = "Hola al Mundo";
    Mayusculas(Cadena);
    cout << Cadena << endl;
    return 0;
}

First of all, you have to pass the address of the string, so instead of char , you have to use char* , like this in your function:

void Mayusculas(char *text)
{
    for(int post = 0; pos < std::strlen(text); pos++)
    {
        text[post] = (char) toupper(text[pos]);
    }
}

Note: The char *text indicated the address of the first character in your string.

The definition in the main function is good, so you can use it like this:

int main() {
  // ...
  char Cadena[100];
  Mayusculas(Cadena);

  std::cout << Cadena << std::endl;
  return 0;
}

I have also written an example that you can execute and test here .

change your code to: as int may not fit into receiver type char

int Mayusculas(char *texto)
{
    int liCount;
    for(liCount=0;liCount<strlen(texto);liCount++)
    {
        texto[liCount]= (char) toupper(texto[liCount]);
    }
}

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