There is an error with my function parameters (handling string)

HI!
This is my code :

1
2
3
4
5
6
7
8
9
10
11
string onetwo (string input)
{
    return "hi" ;
}

int main ()
{
    string input ;
    cin >> input ;
    cout << onetwo (input [0] + input [1]) ;
}


I don't think any additional explanations are needed , when I click the build button , this error appears on line 10:

"invalid conversion from 'int' to 'const char' [-fpermissive]"
Codeblocks 12.11 GCC MinGW
Last edited on
Let consider the expression

input [0] + input [1]

the left and the right operands have type char. According to the C++ Standard when an additive operator is used "
The usual arithmetic conversions are performed for operands of arithmetic or enumeration type.
"
That means that input[0] and input[1] are converted to type int and the result has the same type int. There is no such a constructor in class std::string that has one parameter of type int. So the compiler issues the error.
Also it is not clear what you are trying to do. The function you defined does not use the argument passed to it.
Last edited on
Thanks!
I really appreciate it ;
and forget about the function , it does something important , I just changed it for this forum so that the good people like you don't have problem answering me :))))
Thanks again !
You can do what you want the following way provided that your compiler supports std::initializer_list


cout << onetwo ( { input [0], input [1] } ) ;
Last edited on
Topic archived. No new replies allowed.