error: invalid conversion from 'char' to 'const char*' - c++

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

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.

TL;DR:
Demo using C-style strings
Demo using std::string
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
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.
toupper() returns an int, so you should cast, i.e. change
texto[liCount]=toupper(texto[liCount]);
to
texto[liCount] = (char)toupper(texto[liCount]);
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;
}

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]);
}
}

Related

How to remove whitespace from a expression using a function with const char* pointer

I am working with a small piece of code and want to remove all the whitespaces from a simple expression. I am taking char array expression and passing it to an function with const char* pointer,but i am not able to remove the whitespace. here is what i tried, but didn't get desired output.
#include <iostream>
using namespace std;
void evaluate(const char *expression){
if (*expression == '\0') cout<<"Invalid"<<endl;
while(*expression){
if(*expression == ' '){
*expression++ ;
}
expression++;
}
cout<<expression;
}
int main()
{
char expr[] = "1 + 2 * 3";
evaluate(expr);
return 0;
}
It will be great if someone can help me with this.
Thanks in advance.
Use std::string and the erase-remove idiom
#include <iostream>
#include <string>
void evaluate(const char *expression){
if (*expression == '\0') std::cout << "Invalid\n";
std::cout << expression;
}
int main()
{
char expr[] = "1 + 2 * 3";
std::string exprString = expr;
exprString.erase(
std::remove(std::begin(exprString), std::end(exprString), ' '),
std::end(exprString));
evaluate(exprString.c_str());
return 0;
}
In this code I added string but I recommend to completely remove the C-strings in this code and replace them by std::string.
You can't change the C-string inside the function because it's const.
If I understand your intent is simply to output the expression without spaces when calling evaluate(), you can simplify things by outputting each character that is not a space with:
#include <iostream>
using namespace std;
void evaluate (const char *expression) {
if (!*expression) {
cout << "Invalid\n";
return;
}
while (*expression) {
if (*expression != ' ')
cout << (char)*expression;
expression++;
}
cout << '\n';
}
int main() {
char expr[] = "1 + 2 * 3";
evaluate(expr);
}
Example Use/Output
$ ./bin/rmws_const_char
1+2*3
*expression++
This only increments the pointer (and needlessly evaluates what it was pointing at). It does not modify the original character array.
If you want the expression in memory without spaces, rather than just for output purposes, I suggest that you build a new array with the spaces removed.

Splitting a string into an array of characters using a variable for the string

I'm trying to split a string into an array of individual characters. However, I would like the string to be input by the user, for which I need to define the string using a variable.
My question is, why does this work:
#include <iostream>
using namespace std;
int main() {
char arr [] = {"Giraffe"};
cout << arr[0];
return 0;
}
But this doesn't?
#include <iostream>
using namespace std;
int main() {
string word;
word = "Giraffe";
char arr [] = {word};
cout << arr[0];
return 0;
}
Thanks
Your example doesn't work because you're trying to put a std::string into an array of char. The compiler will complain here because std::string has no type conversion to char.
Since you're just trying to print the first character of the string, just use the array accessor overload of std::string, std::string::operator[] instead:
std::string word;
word = "Giraffe";
std::cout << word[0] << std::endl;
In your second example, the type of word is a std::string and there are no default type conversions from std::string to the type char.
On the other hand, the first example works because it can be interpreted as an array of char (but actually its just c-style const char *).
If, for some reason, you would want to convert std::string into the c-style char array, you might want to try something like this...
#include <iostream>
#include <string>
#include <cstring>
int main(void)
{
std::string word;
word = "Giraffe";
char* arr = new char[word.length() + 1]; // accounting for the null-terminating character
strcpy(arr, word.data());
std::cout << arr[0] << std::endl;
delete[] arr; // deallocating our heap memory
return 0;
}

string s = "hello" vs string s[5] = {"hello"}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s = "hello";
reverse(begin(s), end(s));
cout << s << endl;
return 0;
}
prints olleh
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s[5] = {"hello"};
reverse(begin(s), end(s));
cout << *s << endl;
return 0;
}
prints hello
Please help me understand why is such difference. I am newbie in c++, I am using c++ 11.
Ok, I corrected to s[5]={"hello"} from s[5]="hello" .
The first is a single string. The second is an array of five strings, and initializes all five string to the same value. However, allowing the syntax in the question is a bug (see the link in the comment by T.C.) and should normally give an error. The correct syntax would have the string inside braces, e.g. { "hello" }.
In the second program you are only printing one string of the five anyway, the first one. When you dereference an array, it decays to a pointer and gives you the value that pointer points to, which is the first element in the array. *s and s[0] are equivalent.
I think that what you are looking for is this:
int main() {
char s[] = "hello";
reverse(s, s + (sizeof(s) - 1));
cout << string(s) << endl;
return 0;
}
With char[6] you have an C-style string. Remember that theses strings must be terminated with '\0'. Therefore there is a 6th element.

Is there a quick way to check if a string is numeric?

Is it possible to check if a string variable is entirely numeric? I know you can iterate through the alphabets to check for a non-numeric character, but is there any other way?
The quickest way i can think of is to try to cast it with "strtol" or similar functions and see whether it can convert the entire string:
char* numberString = "100";
char* endptr;
long number = strtol(numberString, &endptr, 10);
if (*endptr) {
// Cast failed
} else {
// Cast succeeded
}
This topic is also discussed in this thread: How to determine if a string is a number with C++?
Hope this helps :)
#include <iostream>
#include <string>
#include <locale>
#include <algorithm>
bool is_numeric(std::string str, std::locale loc = std::locale())
{
return std::all_of(str.begin(), str.end(), std::isdigit);
}
int main()
{
std::string str;
std::cin >> str;
std::cout << std::boolalpha << is_numeric(str); // true
}
You can use the isdigit function in the ctype library:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char mystr[]="56203";
int the_number;
if (isdigit(mystr[0]))
{
the_number = atoi (mystr);
printf ("The following is an integer\n",the_number);
}
return 0;
}
This example checks the first character only. If you want to check the whole string then you can use a loop, or if its a fixed length and small just combine isdigit() with &&.

atoi() conversion error

atoi() is giving me this error:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
from this line:
int pid = atoi( token.at(0) );
where token is a vector
how can i go around this?
token.at(0) is returning a single char, but atoi() is expecting a string (a pointer to a char.) Either convert the single character to a string, or to convert a single digit char into the number it represents you can usually* just do this:
int pid = token.at(0) - '0';
* The exception is when the charset doesn't encode digits 0-9 in order which is extremely rare.
You'll have to create a string:
int pid = atoi(std::string(1, token.at(0)).c_str());
... assuming that token is a std::vector of char, and using std::string's constructor that accepts a single character (and the number of that character that the string will contain, one in this case).
Your example is incomplete, as you don't say the exact type of the vector. I assume it is std::vector<char> (that, perhaps, you filled with each char from a C string).
My solution would be to convert it again on char *, which would give the following code:
void doSomething(const std::vector & token)
{
char c[2] = {token.at(0), 0} ;
int pid = std::atoi(c) ;
}
Note that this is a C-like solution (i.e., quite ugly in C++ code), but it remains efficient.
const char tempChar = token.at(0);
int tempVal = atoi(&tempChar);
stringstream ss;
ss << token.at(0);
int pid = -1;
ss >> pid;
Example:
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
using namespace std;
vector<char> token(1, '8');
stringstream ss;
ss << token.at(0);
int pid = -1;
ss >> pid;
if(!ss) {
cerr << "error: can't convert to int '" << token.at(0) << "'" << endl;
}
cout << pid << endl;
return 0;
}