2

I've got a function which takes a string, let's call it

void print(std::string myString) 
{ 
    std::cout << myString;
}

I want to do something like

char myChar;
myChar = '{';
print("Error, unexpected char: " + myChar + "\n");

It doesn't work.

I tried something like

print(std::string("Error, unexpected char") + std::string(myChar) + std::string("\n) )

but then std::string(myChar) becomes whatever int the char represents, it's printed as an int and isn't printed as it's alphanumeric representation!

5
  • Did you try : std::string("Error, unexpected char") + ch? Commented Apr 18, 2020 at 12:35
  • 1
    There is no constructor of std::string which converts a char into its digits. Did you perhaps use std::to_string? Commented Apr 18, 2020 at 12:38
  • @RobertAndrzejuk i did, but i wanted the newline in there instead of putting it in my print function. Also, yeah I used tostring forgot sorry Commented Apr 18, 2020 at 12:40
  • If ... +ch+"\n" doesn't work, then the newline can be written as: "\n"s Commented Apr 18, 2020 at 12:45
  • @ShazamoMorebucks it's printed as an int and isn't printed as it's alphanumeric representation! -- I'm surprised no one so far has actually explained the issue. You used the wrong std::string constructor. It should have been std::string(1, myChar); Commented Apr 18, 2020 at 13:15

3 Answers 3

2

The function should be declared like:

void print( const std::string &myString) 
{ 
    std::cout << myString;
}

and called like:

print( std::string( "Error, unexpected char: " ) + myChar + "\n");

As for your comment:

as a follow up, would it have been possible to pass an anonymous function returning a string as an argument to print()? Something like print( {return "hello world";}

then you can do this as it is shown in the demonstration program:

#include <iostream>
#include <string>

void f( std::string h() )
{
    std::cout << h() << '\n';
}

int main() 
{
    f( []()->std::string { return "Hello World!"; } );
    
    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

It works! Looks like what I did wrong was passing print(std::string("Unexpected Character " + character) + '\n') as the argument, i should have kept the literal and character separate
as a follow up, would it have been possible to pass an anonymous function returning a string as an argument to print()? Something like print( [](){return "hello world";})
@ShazamoMorebucks Here you are #include <iostream> #include <string> void f( std::string h() ) { std::cout << h() << '\n'; } int main() { f( []()->std::string { return "Hello World!"; } ); return 0; }
Thanks! So I have to declare an overload to accept a lambda each time? Is there a way to automatically accept lambdas for any argument by default?
@ShazamoMorebucks If types of lambda expressions converted to functions are different then you have to declare a template function.
2

If you are using C++14, you can do this:

using namespace std::literals;
char myChar;
myChar = '{';
print("Error, unexpected char: "s + myChar + "\n"s);

1 Comment

Thanks! I'm new to c++ so didn't know you could make string literals, thought u always had char array literals
1

You can convert any one and concat.

You can use str.c_str() to convert C++ string to C character array.

Or

Use std::string inbuilt constructor to convert C character array to C++ string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.