hisoka Posted April 30, 2015 Share Posted April 30, 2015 cout in c++ can be used like this : int main(){cout << "green" << "land" << endl;system("pause"); } or like this int main(){cout << "green" ; cout << "land"<<endl ;system("pause"); } output : greenland However I want the output to be green land but I cannot figure out How . I want a space between the two word using cout So how? any idea ? Link to comment Share on other sites More sharing options...
Ingolme Posted April 30, 2015 Share Posted April 30, 2015 Try cout << " " 1 Link to comment Share on other sites More sharing options...
Don E Posted April 30, 2015 Share Posted April 30, 2015 You can do something like for example: cout << "green " << "land" << endl; or cout << "green" << " " << "land" << endl; 1 Link to comment Share on other sites More sharing options...
hisoka Posted April 30, 2015 Author Share Posted April 30, 2015 Thanks for both of you it works I tried another thing : this works too int main(){string m = "green";string n = "land";cout<< m + " " + n <<endl;system("pause"); } Now I tried it like that : int main(){cout<< "green" + " " + "land" <<endl;system("pause"); } but it gives an error . 8 C:da.cpp invalid operands of types `const char[6]' and `const char[2]' to binary `operator+' I could not understand the error : Any explanation ? and how to correct the little piece code ? Link to comment Share on other sites More sharing options...
hisoka Posted April 30, 2015 Author Share Posted April 30, 2015 I can simply use cout<<"green land"<<endl; and that is all . Why am I complicating things !! it gives green land Link to comment Share on other sites More sharing options...
Ingolme Posted April 30, 2015 Share Posted April 30, 2015 In C++. the + operator only is used to add numbers. It can't do anything with strings. Link to comment Share on other sites More sharing options...
hisoka Posted April 30, 2015 Author Share Posted April 30, 2015 "the + operator only is used to add numbers. It can't do anything with strings" int main(){string man = "py";string woman = "thon";cout<<man+woman<<endl;system("pause");} output python it concatenates string so it is not only used for math operations but for string concatenation too what do you think? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 30, 2015 Share Posted April 30, 2015 These variables: string m = "green";string n = "land";are strings. When you do this: cout<< "green" + " " + "land" <<endl;those are not strings, they are character array constants, like the error message points out: 8 C:da.cpp invalid operands of types `const char[6]' and `const char[2]' to binary `operator+'Those are also referred to as C-style strings. C does not have strings, so a string in C is represented as an array of characters terminated by null (which is why the lengths are 6 and 2 instead of 5 and 1, the extra character is a null). You can cast either one as a C++ string and it will work, but like you said there is no point in concatenating 2 literals. 1 Link to comment Share on other sites More sharing options...
hisoka Posted May 3, 2015 Author Share Posted May 3, 2015 Very good . Now i understand the meaning of the error Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now