0

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*'

ANDY
  • 15
  • 3
  • What exactly is the problem? you mention that "I'm block(ed) with this error" - what is the error? – shapiro yaacov Oct 11 '15 at 04:43
  • 1
    "this error"? well, next time post it. – Karoly Horvath Oct 11 '15 at 04:43
  • It is said in the title @Shapiroyaacov **error: invalid conversion from 'char' to 'const char*'** – ANDY Oct 11 '15 at 04:53
  • Sorry, I thought that the title **error: invalid conversion from 'char' to 'const char*'** was explicit enough @KarolyHorvath. – ANDY Oct 11 '15 at 04:55
  • @ANDY it is explicit enough, actually. Although, one might argue it's also pretty clearly telling you what went wrong. – geometrian Oct 11 '15 at 05:11
  • I'm confused: you accepted vishal's answer, but it has errors, and it's still not working for you...? Please see my answer, which includes links to working demos. – cp.engr Oct 11 '15 at 05:46

3 Answers3

0

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, i.e. 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;
}
Community
  • 1
  • 1
cp.engr
  • 2,291
  • 4
  • 28
  • 42
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.

Victor
  • 13,914
  • 19
  • 78
  • 147
-1

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]);
    }
}
vishal
  • 2,258
  • 1
  • 18
  • 27
  • The same error, is still apearing. I doesn't need to change anything of the main? – ANDY Oct 11 '15 at 04:46
  • @vishal, your suggestions for changes to main() are all incorrect. `char *Cadena[100];` is an array of `char *`s, not `char`s. You do not need to have the [100] in `Mayusculas(Cadena);` (if the compiler even allows it). And `cout<<*Cadena< – cp.engr Oct 11 '15 at 05:22